userxöa userxöa - 9 months ago 35
PHP Question

PHP repeat action with adding value

I am just learning coding, and couldn't find solution for that:

I'm creating a website that shows pictures that are on its server.

$number = $_POST["NR"];

$picture1 = "{$number}.jpg";
echo '<img src="pictures/' . $picture1 . ' ">';

How can I make it to repeat the echo string, but every time add for "$picture1" calculation +1 ? So, that the maximum count would be 40?

Thanks for helping. I know that it is actually easy, but I still don't know how to do it :)


Assuming that you have images named correctly awaiting this to happen:

$number = $_POST["NR"];
$i = 0;
while ($i < 40) {
    $picture = $number + $i.".jpg";
    echo '<img src="pictures/' . $picture . ' ">';

Use a counter and loop. The issue here is that your $number could be anything... For instance if someone passed 500, you would need images named, 500.jpg, 501.jpg, etc. all the way up to 540.jpg. Unless I misunderstood the question.