简体   繁体   English

创建带有计数PHP的图像文件

[英]Creating an image file with count PHP

For all of the records my code loops through them displaying them each in turn. 对于所有记录,我的代码循环遍历它们,依次显示每个记录。 Now I want to add an image for each of them. 现在,我要为每个图像添加一个图像。

To do this I am going to use the file path not BLOB because I can't for this project. 为此,我将使用文件路径而不是BLOB,因为我不能用于该项目。

So far I have the code posted below but I am struggling to implement the count function as I want to the number of the file to increment each time. 到目前为止,我已经在下面发布了代码,但是由于我希望每次增加文件的数量都在努力实现count函数。 My files are stored as images/Starter1.jpg, images/Starter2.jpg etc. 我的文件存储为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 --> 

At each iteration you're putting back $count variable to 0. "Initialize" the variable outside of the loop the increment would work. 在每次迭代时,您都将$ count变量放回0。在循环外“初始化”变量,增量将起作用。

You could also increment in a more nice way than $count = $count + 1; 您也可以用比$count = $count + 1;更好的方式递增$count = $count + 1; by just doing ++$count; 只做++$count;

And don't forget to put quote in the html properties. 并且不要忘记在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 --> 

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

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