创建计数为 PHP 的图像文件

Creating an image file with count PHP

对于所有记录,我的代码循环遍历它们,依次显示它们。现在我想为他们每个人添加一个图像。

为此,我将使用文件路径而不是 BLOB,因为我不能用于此项目。

到目前为止,我已经在下面发布了代码,但我正在努力实现计数功能,因为我希望文件的数量每次都递增。我的文件存储为 images/Starter1.jpg、images/Starter2.jpg 等

<div id = "starters">
    <p class = "big"> Starters </p>
    <?php
    while($row = mysqli_fetch_assoc($result_starters)){
        $count = 0; 
        echo "<div id = item> ".
        "<p>".
        "<b>Name: </b> ". $row["name"].
        "<img src = images/Starter".$count.".jpg width = 100px, height = 100px>".
        "<br><br><b>Price: </b>&pound;". $row["price"].
        "<br><br><a href=menuInfo.php?ID=".$row["productID"]."><button type = button> See more details </button></a>".
        "<br><br><button type = button> Add to favourites </button>".
        "<br><br><button type = button> Add to basket </button>".   
        "</p>". 
        "</div>";
        $count = $count + 1;
    }
    ?>
</div> <!-- For starters --> 

在每次迭代中,您将 $count 变量放回 0。"Initialize" 循环外的变量增量将起作用。

您还可以通过 ++$count;

以比 $count = $count + 1; 更好的方式递增

并且不要忘记在 html 属性中加上引号。

<div id = "starters">
    <p class = "big"> Starters </p>
    <?php
    $count = 0; 
    while($row = mysqli_fetch_assoc($result_starters)){
        ?>
        <div id ="item">
            <p>
                <b>Name: </b><?php echo $row["name"]; ?>
                <img src="images/Starter<?php echo $count; ?>.jpg" width="100px" height="100px">
                <br><br><b>Price: </b>&pound;<?php echo $row["price"]; ?>
                <br><br><a href="menuInfo.php?ID=<?php echo $row["productID"]; ?>"><button type="button"> See more details </button></a>
                <br><br><button type="button"> Add to favourites </button>
                <br><br><button type="button"> Add to basket </button>
            </p>
        </div>
    <?php
        ++$count;
    }
    ?>
</div> <!-- For starters -->