Jump to content

randomly selected pictures are the same


brem13

Recommended Posts

hey, im trying to get pictures to display randomly from a mysql database but the same ones come up together. i have 9 displayed on a page and a lot of times the same pic will show up 3 or 4 times on the same page. i have this code 9 times in my script for each table cell. can someone show me how to make it so every picture is random and the same ones arent displayed more than once?

<?
include("config.php");
mysql_connect($server, $db_user, $db_pass) or die (mysql_error()); 
$result = mysql_db_query($database, "select * from $table ORDER BY rand() limit 1") or die (mysql_error());
while($qry = mysql_fetch_array($result))
{ 
	$user = $qry['username'];
	$pic = $qry['mainPic'];
}//end while
list($width, $height, $type, $attr) = getimagesize("userImages/thumbs/".$pic);

if($height>=101)
{
	echo "<img src=userImages/thumbs/$pic height=100px> <font face=tahoma size=2>$user</font><br>";
}//end if $height
else
{
	echo "<img src=userImages/thumbs/$pic width=100px> <font face=tahoma size=2>$user</font><br>";
}
echo "<font face=tahoma size=1>Last Online - Looking For</font>";
?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/196485-randomly-selected-pictures-are-the-same/
Share on other sites

Well first off unless you have well over a 100 pics that is random... and even if you have a 100 there is still like roughly a 10% chance that you will have repeats.

 

But to fix this issue, with each time the code runs have an array that carries the names of the pictures of the randomly chosen picture.  Fix with a loop and the array

 

Just thinking this as I go so check my logic first

Run random code in a loop... 
get random pic name.... 
check if name of pic is in array of already used pics(array will be empty or preferably for security reasons set to a dummy variable).... 
if no matches then display pic... add to pic array and exit loop.... 
if is match then loop back to a random code to get a different... and start at step one

im not too knowledgeable with arrays, would it be something like this?

foreach($result as $aPic)
{
$picArray = array($pic)
}

 

with a foreach loop it works only on arrays in the example below $my_array is the array to store the pics used... $my_array_element is used so that on each loop through the array the current element is set to it.... so chech $my_array_element against the randomly chosen picture.

foreach($my_array as $my_array_element)
{
   if($my_array_element == $random_pic)
     {
        //ok its the same we have to run random again
      }
  else
    {
      // Not same, sweet lets do something here
     // PRINT PIC HERE TO SCREEN
     array_push($my_array,$random_pic); //Adds pic to end of array
     continue; // exits and continues the program without going through rest of array.
    }
}

Put together something like this... not tested, but see if it works?

 

<?
    //////////////// SETS VARIABLES DO NOT INCLUDE THIS IF YOU USES THIS CODE mulitple times, we don't want to reset our array///
$my_array = 0; // just to dummy set variable
$my_ender = 0; // used for loop
///////////////////////////////////////////////////////////////////////////////

   include("config.php"); 
   mysql_connect($server, $db_user, $db_pass) or die (mysql_error());
   
while($my_ender == 0)
  {
  
   $result = mysql_db_query($database, "select * from $table ORDER BY rand() limit 1") or die (mysql_error());
   while($qry = mysql_fetch_array($result))
   {
      $user = $qry['username'];
      $pic = $qry['mainPic'];
   }//end while
   
   $random_pic = $pic;
   foreach($my_array as $my_array_element)
{
   if($my_array_element == $random_pic)
     {
        //ok its the same we have to run random again
      }
  else
    {
      // Not same, sweet lets do something here
     ///////////////////////////////////////////////////////////////
     
     //////// DO YOUR STUFF YOU WROTE HERE ///////////////
     list($width, $height, $type, $attr) = getimagesize("userImages/thumbs/".$pic);
         
   	if($height>=101)
   	{
      echo "<img src=userImages/thumbs/$pic height=100px> <font face=tahoma size=2>$user</font><br>";
   	}//end if $height
   	else
   	{
      echo "<img src=userImages/thumbs/$pic width=100px> <font face=tahoma size=2>$user</font><br>";
   	}
   echo "<font face=tahoma size=1>Last Online - Looking For</font>";
         
     ///////////////////////////////////////////////////////////////
     
     array_push($my_array,$random_pic); //Adds pic to end of array
     $my_ender = 1;
     continue; // exits and continues the program without going through rest of array.
    }
}
   

   
  }// end of while loop $my_ender
  $my_ender = 0; // resets $my_ender for later use
  
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.