简体   繁体   English

我如何生成将根据 ID 从数据库显示这些产品的链接到 php 中的另一个页面(产品页面)

[英]How do i generate links that will display these products depending on the ID from the database to another page (product page) in php

<?php                      
    foreach($products as $i => $product): ?>
    <article class="products__card">
        <a href="./product.php?id">
            <img src="<?php echo $product['image'] ?>" alt="" 
        class="product__image">

            <h3 class="products__title"><?php echo 
            $product['Device_name'] ?></h3>
            <span class="products__price"><?php echo 
        number_format($product['price']) ?> UGX</span>
        </a>
        <button class="products__button">
        <i class="uil uil-shopping-bag"></i>
        </button>
        <input type="hidden" name="id" value="<?php echo $product['id'] 
                    ? 
                >">
    </article>
<?php endforeach; ?>

The sql is okay the column for id exists but I am not sure how to generate pages depending on the id of the clicked item. sql 没问题,id 列存在,但我不确定如何根据单击项目的 id 生成页面。

If I understand you, you need to have a link to the selected product, if that is what you asked, you can create it like this:如果我理解您,您需要有一个指向所选产品的链接,如果这是您所要求的,您可以像这样创建它:

product.php?id=<?php echo $product['id']; ?>

With this you will get an example if your product has 412 ID in the database, the link will be https://example.com/product.php?id=412 .如果您的产品在数据库中有 412 ID,您将获得一个示例,链接将是https://example.com/product.php?id=412

And in product.php page you need to do something like this:product.php页面中,您需要执行以下操作:

$link = null;

if(!empty($_GET['id'])) {
        $link = $_GET['id'];
}
if (null === $link) {
    header('index');
} else {
    // code
...

The first line of code prevents users to manually change id number of your product, in that case, they will be automatically redirected to the index page or else the display page.第一行代码防止用户手动更改您的产品的 id 号,在这种情况下,他们将被自动重定向到索引页面或显示页面。 Hope it helps.希望能帮助到你。

The jumping back and forth between PHP mode and HTML mode is inappropriate. PHP 模式和 HTML 模式之间来回跳转是不合适的。
You mention SQL but there is no SQL shown.您提到 SQL 但没有显示 SQL。
Instead of breaking out of PHP into HTML and back just make the strings in PHP and use concatenation to make the string and echo it.而不是将 PHP 分解为 HTML 并返回,只需在 PHP 中制作字符串并使用连接来制作字符串并echo显它。

echo `<a href="./product.php"><img class="product__image" src="' . $product['image'] . '" alt="" />';



foreach works with arrays as key-value pairs. foreach 使用 arrays 作为键值对。 Where i is your key and $products . i是您的密钥和$products If you do not need the key (i) then you do not need to use a key variable.如果您不需要密钥 (i),那么您不需要使用密钥变量。

You should not use associative arrays here.您不应在此处使用关联 arrays。 It is very difficult to embed them in PHP strings.很难将它们嵌入 PHP 字符串中。 I can show you how to do it correctly but for now I will simplify the variables.我可以向你展示如何正确地做到这一点,但现在我将简化变量。 And we can eliminate concatenation.我们可以消除串联。

$image = $product['image'];
$price = number_format($product['price']);
$device = $product['Device_name'];
$id = $product['id'];

This is not valid, it will show nothing in the Browser这是无效的,它不会在浏览器中显示任何内容

<i class="uil uil-shopping-bag"></i>

The "i" tag was used to italicize text: “i”标签用于斜体文本:

<i>The will render in italic print</i> 

The <i> tag was replaced by <span> <i>标签被替换为<span>

The img tag ends with /> . img 标签以/>结尾。 needs the slash as does <input... /><input... />一样需要斜线

foreach($products as $product){
  echo <<<EOT
    <article class="products__card">
        <a href="./product.php?id">
          <img class="product__image" src="$image" alt="" />
          <h3 class="products__title">$device</h3>
          <span class="products__price">$price UGX</span>
        </a>
        <button class="products__button"></button>
        <input type="hidden" name="id" value="$id" />
    </article>
EOT;
}

Now the link to the product page: I saw you used and anchor a href="./product.php">现在是产品页面的链接:我看到你使用并锚定a href="./product.php">

And you used <input type="hidden" name="id" value="$id" />而你使用<input type="hidden" name="id" value="$id" />

You should use a <form> and with a POST rather than an anchor GET您应该使用<form>并使用 POST 而不是锚点 GET

<form action="./product.php? method="post"><button><img src="icon.jpg" width="20" height="20" /></button><input ="hidden" name="id" value="$id"</form>

NOTE: Always specify a width and height in a <img> .注意:始终在<img>中指定widthheight If you do not, it gives the Browser a lot of unnecessary work to do.如果你不这样做,它会给浏览器带来很多不必要的工作。

Now the associative array.现在是关联数组。 I assume you got that from mySQL.我假设你是从 mySQL 那里得到的。
This is how I get the column contents.这就是我获取列内容的方式。
I use the fetch_array and specify only the numeric array.我使用 fetch_array 并仅指定数字数组。
I get the numeric array because the list() function will assign the value of a numeric array to simple variable.我得到了数值数组,因为list() function 会将数值数组的值分配给简单变量。

$sql = "SELECT `id`,`Device_name`,`price`,`image` FROM `Products` WHERE 1";
$results = mysqli_query($conn,$sql);
while(list($id,$device,$price,$image) =  mysqli_fetch_array($results, MYSQLI_NUM)){
  echo <<<EOT
<article class="products__card">
<img class="product__image" src="$image" alt="" />
<h3 class="products__title">$device</h3>
<span class="products__price">$price UGX</span>
<form action="./product.php? method="post"><button><img src="icon.jpg" width="20" height="20" /></button><input ="hidden" name="id" value="$id"</form>
 </article>
EOT;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM