Can someone help me with the code for displaying images from a PHP array at random but with no duplicates?
I really appreciate any help. thanks!
Can someone help me with the code for displaying images from a PHP array at random but with no duplicates?
I really appreciate any help. thanks!
$imgs = array( ... );
shuffle( $imgs );
print array_shift( $imgs ); // repeat.
It is now random with no duplicates…
Thanks for the response!! So I wanted to clarify my problem and maybe you could help clear this up. Here is my code:
<?
$tempHolder = '1';
echo "<table width=\\"90%\\" align=\\"center\\">";
while ($row_Recordset2 = mysql_fetch_assoc($Recordset2))
{
$img_array = array
('001c.jpg',
'002.jpg',
'003b.jpg',
'004b.jpg',
'005b.jpg',
'007b.jpg',
'008a.jpg',
'010a.jpg',
'011a.jpg',
'012.jpg',
'013.jpg',
'014b.jpg',
'015.jpg',
'016d.jpg',
'017a.jpg',
'018b.jpg',
'019b.jpg',
'020b.jpg');
$total2 = count($img_array);
$random2 = (mt_rand()%$total2);
$varImg = "$img_array[$random2]";
echo "<tr>";
echo "<td align=\\"center\\"><img src=\\"http://".$row_Recordset2['Website']."/uploads/".$varImg."\\"></td>";
echo "<tr>";
}
echo "</table>";
?>
So it works but the problem is there are 18 images but around 75 results that come back from the database and many duplicate images get displayed. I need to display 18 images at random from the array of 18 images but never display duplicates… Thanks again for your help!
If you only want to show the 18 rows, put a limit on your query with LIMIT 18 and use this:
<?php
$tempHolder = '1';
$img_array = array('001c.jpg', '002.jpg', '003b.jpg', '004b.jpg', '005b.jpg', '007b.jpg', '008a.jpg', '010a.jpg', '011a.jpg', '012.jpg', '013.jpg', '014b.jpg', '015.jpg', '016d.jpg', '017a.jpg', '018b.jpg', '019b.jpg', '020b.jpg');
shuffle($img_array);
echo '<table width="90%" align="center">';
for($i = 0; $row_Recordset2 = mysql_fetch_assoc($Recordset2); $i++){
echo <<<ROW
<tr>
<td align="center">
<img src="http://{$row_Recordset2['Website']}/uploads/{$img_array[$i]}" />
</td>
</tr>
ROW;
}
echo '</table>';
?>
Thanks man… This is exactly what I needed, most appreciated!
Here’s a second question based off of your answer… I did limit the SQL results to 18 as you suggested, however I actually have a lot more images that I need to display. I CAN have duplicates but just NOT together…
So in other words, there are 100 domains and each of the 100 domains has a set of 18 images with the same name.
I need to shuffle the array and display 18 images with no dups (which you helped me with, thanks), now I need to display the next 18 with the image array shuffled again… and then the next 18 etc…
The net result would be that I can use the 18 images over and over again and yes there would be duplicates but none would be next to eachother (except in the unlikely event that the dups fall at the end and the beginning of the adjacent shuffled arrays which is no big deal).
Does this make sense? Thanks again for your awesome help!
Mitch
Well, you have two choices. One is to display the images again in the same order (take the limit off the query):
<?php
$tempHolder = '1';
$img_array = array('001c.jpg', '002.jpg', '003b.jpg', '004b.jpg', '005b.jpg', '007b.jpg', '008a.jpg', '010a.jpg', '011a.jpg', '012.jpg', '013.jpg', '014b.jpg', '015.jpg', '016d.jpg', '017a.jpg', '018b.jpg', '019b.jpg', '020b.jpg');
shuffle($img_array);
echo '<table width="90%" align="center">';
for($i = 0; $row_Recordset2 = mysql_fetch_assoc($Recordset2); $i++){
if($i == 18){
$i = 0;
}
echo <<<ROW
<tr>
<td align="center">
<img src="http://{$row_Recordset2['Website']}/uploads/{$img_array[$i]}" />
</td>
</tr>
ROW;
}
echo '</table>';
?>
or shuffle the array again:
<?php
$tempHolder = '1';
$img_array = array('001c.jpg', '002.jpg', '003b.jpg', '004b.jpg', '005b.jpg', '007b.jpg', '008a.jpg', '010a.jpg', '011a.jpg', '012.jpg', '013.jpg', '014b.jpg', '015.jpg', '016d.jpg', '017a.jpg', '018b.jpg', '019b.jpg', '020b.jpg');
shuffle($img_array);
echo '<table width="90%" align="center">';
for($i = 0; $row_Recordset2 = mysql_fetch_assoc($Recordset2); $i++){
if($i == 18){
$i = 0;
shuffle($img_array);
}
echo <<<ROW
<tr>
<td align="center">
<img src="http://{$row_Recordset2['Website']}/uploads/{$img_array[$i]}" />
</td>
</tr>
ROW;
}
echo '</table>';
?>
Many thanks arkinstall… That worked perfectly.