简体   繁体   中英

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.

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. My files are stored as images/Starter1.jpg, images/Starter2.jpg etc.

<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.

You could also increment in a more nice way than $count = $count + 1; by just doing ++$count;

And don't forget to put quote in the html properties.

<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 --> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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