繁体   English   中英

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

[英]Creating an image file with count PHP

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

为此,我将使用文件路径而不是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>&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。在循环外“初始化”变量,增量将起作用。

您也可以用比$count = $count + 1;更好的方式递增$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>&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