please look the following code:
<?php
if ($_GET['picture'] == 1) {
echo "<img src=\"1.jpg\" />";
} else if ($_GET['picture'] == 2) {
echo "<img src=\"2.jpg\" />";
} else if ($_GET['picture'] == 3) {
echo "<img src=\"3.jpg\" />";
}
?>
Now, when I click a link like index.php?picture=1, the picture 1.jpg will appear. But, when I have a thousand of these, I don't want to create every If statement myself: so how to I loop through this? How can I create a loop so these are automatically created?
You can include the value inside of the echo'd string. Just check to make sure its less than your maximum (1000 or whatever):
$picture = intval($_GET['picture']);
if (picture > 0 && $picture < maximum) {
echo "<img src=\"{$picture}.jpg\" />";
}
You can try this, make sure to stereotype the $_GET value as an integer or you can have some serious XSS problems.
<?php
$picnum=(int)$_GET['picture']; // make sure the value is a number to avoid XSS
echo "<img src='".$picnum.".jpg' />";
?>
$picture = isset($_GET['picture']) ? (int)$_GET['picture'] : 0;
if($picture)
echo '<img src="'.$picture.'.jpg" />';