Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting 3 random images 1

Status
Not open for further replies.

bdichiara

Programmer
Oct 11, 2006
206
US
I have a complicated situation I was wondering if someone could make it look simple. :)

I have several folders with thumbnail images in them. I have them setup like this:
client_name/thumbs/img.gif

I store all the "client_name"s in a table (called clients). This is important for I would like to reference the ID of that client to go to their page.

Here's the catch: some clients don't have any thumbnails in their folder (or may not even have a folder).

Anyway, I need to pull 3 random images from 3 random client's thumbnail folder and also along with it, link to that client's details (details.php?c=ID).

Any ideas? (site:
_______________
_brian.
 
how about this:

1. recursively walk the directories and store the filenames and paths in a numeric array

2. grab three random integers from the universe of array positions
Code:
for ($i=0; $i<3; $i++) {$x[$i] =rand (0, count($files));}

3. push img tags with the src set to the array element with the random integer.
 
Ok, JP.

This is going well, however I think we need to modify this line:
Code:
$x[$i] =rand (0, count($files));
As I loop through my files, I'm storing the items like this:
Code:
$images = "";
//start loop
   $images .= $row_randoms['clientid'].",";
   $images .= $startdir."thumbs/".$file.";";
//end loop

$images = explode(";",$images);
So now, each element in $images should hold 2 pieces of information: "CLIENTID,IMG_LOCATION"

So what do I need to do to that line so it randomly gets that whole line?

_______________
_brian.
 
Ok, wait, I'm sorry. I figured out what was going on. So, this is now my code:
Code:
for ($i=0; $i<3; $i++) {
   $rand_img[$i] = $images[rand(0, count($images))];
}

Now, is it possible to keep it from repeating the same clients? I would probably need to store my $images array a little better right?

_______________
_brian.
 
Ok, not only do i want to get 3 different clients, but I want all 3 images to be different. Plus, I seem to be getting some blank ones. How do I fix all this?

_______________
_brian.
 
If you visit you will notice:

1. Sometimes the image is missing
2. Sometimes the images are from the same client
3. Sometimes the images are exactly the same images.

here's the code:
Code:
$images = "";
do {
  $startdir = "images/".$row_randoms['folder']."/";
  $handle = $startdir;

  if(!file_exists($startdir)){
    // do nothing
  } else {
    if($handle = opendir($startdir)){
      while (false !== ($file = readdir($handle))) { 
        $filetype = explode(".", $file);
          if ($file != "." && $file != ".." && $file != "index.html" && $file != "index.php" && (strtolower($filetype[1]) == "gif" || strtolower($filetype[1]) == "jpg"  || strtolower($filetype[1]) == "bmp" || strtolower($filetype[1]) == "jpeg")) {
            $images .= $row_randoms['clientid'].",";
            $images .= $startdir."thumbs/".$file.";";
          }
        }
      }
      closedir($handle); 
    }
  }
} while ($row_randoms = mysql_fetch_assoc($randoms));

/*************** OUTPUT THE IMAGES *****************/

$images = explode(";",$images);
$total = count($images);
$listed = array();
for ($i=0; $i<3; $i++) {
  $rand_img[$i] = $images[rand(0, count($images))];
  $details = explode(",",$rand_img[$i]);
  echo '<div style="width:120px; height:120px; background-color:#EEEEEE; border:10px solid #FFFFFF; margin-bottom:20px;">';
  echo '<table width="120" border="0" cellspacing="0" cellpadding="0">';
  echo '<tr><td align="center" valign="middle" height="120">';
  if($details[0] == "" || $details[1] == ""){ 
      echo 'where is my image?';
    } else {
      echo '<a href="details.php?c=';
      echo $details[0];
      echo '" class="image">';
      echo '<img src="';
      echo $details[1];
      echo '" width="118" height="118" border="0"></a>';
    }
  echo '</td></tr></table></div>';
}

_______________
_brian.
 
Basically, determine your client #s first:

Code:
$client1 = rand(0, count($clients));
$client2 = rand(0, count($clients));
while ( $client1 == $client2 )
  $client2 = rand(0, count($clients));
$client3 = rand(0, count($clients));
while ( $client1 == $client3 || $client2 == $client3 )
  $client3 = rand(0, count($clients));

Then do the same thing with the images for the chosen clients.

I just realized that you might want to disqualify clients from the display if they're missing images... in that case:

Code:
$client1 = rand(0, count($clients));
while ( invalid($client1) )
   $client1 = rand(0, count($clients));
$client2 = rand(0, count($clients));
while ( $client1 == $client2 || invalid($client2) )
  $client2 = rand(0, count($clients));
$client3 = rand(0, count($clients));
while ( $client1 == $client3 || $client2 == $client3 || invalid($client3) )
  $client3 = rand(0, count($clients));

"invalid" is a function you'd write to verify that the data you need does exist.
 
Ok, I'm doing something incredibly wrong...

Here's my function:
Code:
function invalid($client){
  if($client[0] == "" || $client[1] == ""){
    return true;
  } else {
    if(count($clients) == 0){
      return false;
    } else {
      if(count($clients) >= 1){
        if(!in_array($client[0],$clients)){
          array_push($clients,$client[0]);
          return false;
        } else {
          return true;
        }
      }
    }
  }
}

And here's where I'm looping through arrays:
Code:
$rand_img1 = explode(",",$images[rand(0, $total)]);
while (invalid($rand_img1)){
  $rand_img1 = explode(",",$images[rand(0, $total)]);
}
$rand_img2 = explode(",",$images[rand(0, $total)]);
while ($rand_img1 == $rand_img2 && invalid($rand_img2)){
  $rand_img2 = explode(",",$images[rand(0, $total)]);
}
$rand_img3 = explode(",",$images[rand(0, $total)]);
while ($rand_img1 == $rand_img3 && $rand_img2 == $rand_img3 && invalid($rand_img3)){
  $rand_img3 = explode(",",$images[rand(0, $total)]);
}

I'm still getting duplicate images, blank ones, and the same clients...

_______________
_brian.
 
Code:
while ($rand_img1 == $rand_img2 || invalid($rand_img2))

Or, not and. If they're equal, keep going. If it's invalid, keep going. When they're not equal and it's valid, stop.
 
Ok, I'm no longer getting duplicates, but my function doesn't seem to be working to check the IDs of the client. I'm getting the same clients on the page... Something must be wrong with the way I'm doing it. I'm not to good at arrays, but this is what I've tried:
Code:
$show_clients = "";
function invalid($client){
  if($client[0] == "" || $client[1] == ""){
    return true;
  } else {
    if(count(explode(",",$show_clients)) == 0){
      $show_clients .= $client[0].",";
      return false;
    } else {
      if(count(explode(",",$show_clients)) >= 1){
        if(!in_array($client[0],explode(",",$show_clients))){
          $show_clients .= $client[0].",";
          return false;
        } else {
          return true;
        }
      }
    }
  }
}

_______________
_brian.
 
Just tidying up a little...
Code:
$show_clients = "";
function invalid($client){
  if($client[0] == "" || $client[1] == ""){
    return true;
  } else {
    $clientarray = explode(",",$show_clients) ;
    $clientcount = count($clientarray) ;
    if($clientcount == 0){ // alternatively, $show_clients==""
      $show_clients = $client[0].",";
      return false;
    } else {
      if( $clientcount >= 1){ // not really necessary, count >= 0 by definition
        if(!in_array($client[0],$clientarray)){
          $show_clients .= $client[0].",";
          return false;
        } else {
          return true;
        }
      }
    }
  }
}

I try not to repeat function calls, personally. Other than that and the couple comments I added, periodically print out $show_clients and see what's going on.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top