Random image check.

images php coding issues or problems here.

Moderators: macek, egami, gesf

New php-forum User
New php-forum User
Posts: 1
Joined: Tue Apr 05, 2011 6:11 pm

Random image check.

Postby ijoek » Tue Apr 05, 2011 6:18 pm


I'm new to PHP so I was wondering if i could get a question answered.

I'm trying to display 5 random images from a yahoo pipes feed I created.

I'm able to get the 5 random images to display from the feed but I was wondering if there is a way to check the output of each so if the same images appears twice in the output the script is run again until it displays an image not currently being displayed.

Either that or a random output generator that displays 5 images that aren't the same.

This what I have so far.

Code: Select all

 <div id="DCfeeds">
    <!-- feed1 PIPE -->
        // Pipes Request
        $req = 'http://pipes.yahoo.com/pipes/pipe.run?_id=3a2cd5ebbce8bd263ba0e491e14ad43f&_render=php';
        // Make the request
        $phpserialized = file_get_contents($req);
        // Parse the serialized response
        $phparray = unserialize($phpserialized);
      // array to hold just the pipe items
        $items = $phparray['value']['items'];
      // get the total number of items in the array
      $items_length = count($items);
        // generate a random number between 0 and the end of the items array
      $randomIndex = rand(0, $items_length-1); // remember the array starts at 0 so the last item index is the total number of items minus 1
      $randomIndex2 = rand(0, $items_length-1);
      $randomIndex3 = rand(0, $items_length-1);
      $randomIndex4 = rand(0, $items_length-1);
      $randomIndex5 = rand(0, $items_length-1);
      // get the data we want for the item at $randomIndex
      $link = $items[$randomIndex]['y:published']['link'];
      $link2 = $items[$randomIndex2]['y:published']['link'];
      $link3 = $items[$randomIndex3]['y:published']['link'];
      $link4 = $items[$randomIndex4]['y:published']['link'];
      $link5 = $items[$randomIndex5]['y:published']['link'];
        $thumbnail_url = $items[$randomIndex]['media:thumbnail']['0']['url'];
      $thumbnail_url2 = $items[$randomIndex2]['media:thumbnail']['0']['url'];
      $thumbnail_url3 = $items[$randomIndex3]['media:thumbnail']['0']['url'];
      $thumbnail_url4 = $items[$randomIndex4]['media:thumbnail']['0']['url'];
      $thumbnail_url5 = $items[$randomIndex5]['media:thumbnail']['0']['url'];
       <h2><a href="">feed1</a></h2>
       <div id="DCbar">
        </div> <!--DCbar-->
        <div id="DCcounter">
        </div> <!--DCcounter-->
        <div id="DCoutput">
        <img src="<?php echo $thumbnail_url; ?>" />
         <img src="<?php echo $thumbnail_url2; ?>" />
         <img src="<?php echo $thumbnail_url3; ?>" />
         <img src="<?php echo $thumbnail_url4; ?>" />
         <img src="<?php echo $thumbnail_url5; ?>" />
        </div> <!--DCoutput-->
    </div> <!--DCfeeds-->

User avatar
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Random image check.

Postby egami » Tue Apr 05, 2011 7:26 pm


So, you're almost there.

simply create an array to hold your five variables..

Code: Select all

// grab your pictures from yahoo here..


$need = 5; // change it on the fly if you want..  
while (count($array) <= $need) { // since the count of array is less than or equal to the need..
  $random = rand(0,($items_length - 1)); // pick the random number between 0 and max items - 1
  if (!in_array($random,$array)) {  // if our random number isn't in our storage array.. 
    $array[]=$random; // stuff it! Now it is! (count goes up on next cycle.. when it reaches $need, it quits.

//wrap your div's around this..
foreach($array as $key => $value) { //cycle through the array to create your 5 links, dynamically. 
   echo '<img src="'.$items[$value]['media:thumbnail']['0']['url'].'" border="0">';
//close your divs around this..

Return to “PHP coding => Images”

Who is online

Users browsing this forum: No registered users and 2 guests