对于所有记录,我的代码循环遍历它们,依次显示它们。现在我想为每个人添加一个图像。
要做到这一点,我将使用文件路径而不是BLOB,因为我不能用于此项目。
到目前为止,我已经在下面发布了代码,但我正在努力实现count函数,因为我想要每次递增的文件数。我的文件存储为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>£". $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 -->
答案 0 :(得分:2)
在每次迭代中,你将$ count变量放回到0.“初始化”循环外部的变量,增量将起作用。
只需执行$count = $count + 1;
++$count;
更好的方式增量
不要忘记在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>£<?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 -->