htcilt
03-25-2010, 05:49 PM
I'm generating a random image using an array e.g.
$pics = array(
'image1',
'image2',
'image3'
);
$randompic = $pics[array_rand($pics)];
echo "<img src=\"images/".$randompic.".jpg\" />";
On the page that displays the image (lets call it index.php) I echo different content based on the parameter in the URL e.g.
index.php?cat=group1
I now need to do the same for the random image generation.
I can use $_GET in randomimage.php to select a random image based on the group e.g.
$category = $_GET['cat'];
$group1 = array(
'image1',
'image2',
'image3'
);
$group2 = array(
'image4',
'image5',
'image6'
);
if ($category=='group1'){
$randompic = $group1[array_rand($group1)];}
elseif ($category=='group2'){
$randompic = $group2[array_rand($group2)];}
echo "<img src=\"images/".$randompic."\" />";
However, I dont know how to do the echo on index.php rather than randomimage.php.
I tried
echo "<img src=\"randompic.php?cat=group1\" />";
but looking at the html, it shows the above as a literal string.
I'm guessing I need to set the header on randomimage.php to header("Content-type: image/JPEG"); to just display the image with no html?
$pics = array(
'image1',
'image2',
'image3'
);
$randompic = $pics[array_rand($pics)];
echo "<img src=\"images/".$randompic.".jpg\" />";
On the page that displays the image (lets call it index.php) I echo different content based on the parameter in the URL e.g.
index.php?cat=group1
I now need to do the same for the random image generation.
I can use $_GET in randomimage.php to select a random image based on the group e.g.
$category = $_GET['cat'];
$group1 = array(
'image1',
'image2',
'image3'
);
$group2 = array(
'image4',
'image5',
'image6'
);
if ($category=='group1'){
$randompic = $group1[array_rand($group1)];}
elseif ($category=='group2'){
$randompic = $group2[array_rand($group2)];}
echo "<img src=\"images/".$randompic."\" />";
However, I dont know how to do the echo on index.php rather than randomimage.php.
I tried
echo "<img src=\"randompic.php?cat=group1\" />";
but looking at the html, it shows the above as a literal string.
I'm guessing I need to set the header on randomimage.php to header("Content-type: image/JPEG"); to just display the image with no html?