简体   繁体   English

为什么我的 PHP 代码不重复 HTML 元素?

[英]Why does my PHP code not repeat HTML elements?

In this block of code, you will see that I have used HTML to create an item template.在这段代码中,您将看到我使用 HTML 创建了一个项目模板。 Then I have used PHP to create a while loop to repeat the template for every record in my database.然后,我使用 PHP 创建了一个 while 循环,为我的数据库中的每条记录重复模板。 (I use phpmyadmin to control my SQL database) (我使用 phpmyadmin 来控制我的 SQL 数据库)
However, the issue is that the template doesn't repeat for every record in the database although it does show for the first record in the database but not the rest.但是,问题是模板不会为数据库中的每条记录重复,尽管它确实显示了数据库中的第一条记录,但不显示 rest。 Now I wonder what I should do?现在我想知道我该怎么办? The code without the CSS is down below, Thank you!没有 CSS 的代码在下面,谢谢!

<!DOCTYPE html>
<html lang="en">

<head>
    <script src="https://kit.fontawesome.com/9114d9acc8.js" crossorigin="anonymous">

    </script>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="includes/navbar/navbar.css">
    <link rel="stylesheet" href="css/style.css">
    <title>Document</title>
</head>

<body>
    <?php include "includes/navbar/navbar.php"; ?>
    <?php
    require 'includes/dbh.php';
    $query = sprintf('SELECT * FROM produce');
    $result = mysqli_query($conn, $query);
    while ($row = mysqli_fetch_assoc($result)) { 
    ?>
    <div class="itemTemplate">
        <a id="title" href="register.php"><?php echo $row['Name'] ?></a>
        <a href=""><img src="<?php echo $row['img'] ?>" alt="<?php echo $row['Name'] ?>"></a>
        <span>Price: £<?php echo $row['Price'] ?></span>
        <div class="flex-b">
            <label for="">Quantity:</label>
            <select name="" id="">
                <option>1</option>
                <option>2</option>
                <option>3</option>
                <option>4</option>
                <option>5</option>
                <option>6</option>
                <option>7</option>
                <option>8</option>
            </select>
            <a href="signIn.php"><i class="fas fa-shopping-basket"></i></a>
        </div>
    
    <?php 
        }
    ?>
    </div>

</body>
</html>

This is the way I'd do it.这是我会做的方式。 Some personal preference but I find it much better echoing statments as the HTML is neater and more manageable.一些个人喜好,但我发现它更好地呼应陈述,因为 HTML 更整洁,更易于管理。

 <?php
 $select = mysqli_query($conn,"select * from produce");
 while ($row=mysqli_fetch_array($select)) {

 // Assign variables to table indexes
 $img = $row['img'];
 $name = $row['name'];
 $price = $row['price'];

 // Echo statement in while loop
 echo "
 <div class='itemTemplate'>
 <a id='title' href='register.php'>$name</a>
 <a href=''><img src='$img' alt='$name'></a>
 <span>Price: £$price</span>
 <div class='flex-b'>
 <label for=''>Quantity:</label>
 <select name='' id=''>
 <option>1</option>
 <option>2</option>
 <option>3</option>
 <option>4</option>
 <option>5</option>
 <option>6</option>
 <option>7</option>
 <option>8</option>
 </select>
 <a href='signIn.php'><i class='fas fa-shopping-basket'></i></a>
 </div>
 ";

 // End while loop after looped HTML content.
 }
 ?>

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

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