1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Random images for one day??

Discussion in 'PHP' started by 123GoToAndPlay, Mar 18, 2006.

  1. #1
    Hi all,

    I am trying to get three random images displayed for a day.

    Getting 3 random images is no problem I am using this query
    
    $sql = "SELECT * FROM images ORDER BY RAND() LIMIT 0,3";
    
    Code (markup):
    but I want the random three to be displayed for a day or weekday.

    How can i do this??
     
    123GoToAndPlay, Mar 18, 2006 IP
  2. rvarcher

    rvarcher Peon

    Messages:
    69
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Here's the logic I came up with.

    Create a seperate table in your DB.

    Create 2 fields in the table. One an ID field. One for serialized random image array.

    Your random image array will look like this:
    $random_image[date]
    $random_image[pic1_id]
    $random_image[pic2_id]
    $random_image[pic3_id]

    $random_image = unserialize(your query result here.);

    here's the cool part.

    If ( todays_date != $random_image[date] )
    {
    then get 3 new random images

    update my random_image array with todays date and the 3 new image ids.

    serialize the array and store in my table
    }

    On the date part you could just look at todays value. like 18.

    $today = date("j");
     
    rvarcher, Mar 18, 2006 IP
  3. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    hi rvacher,

    wow thks, but I almost got it working, the idea is the same as yours
    
    $ids_today = array();
    // first check if todays images ids are not already stored
    $check_today_sql = "SELECT todays_id FROM rand_images WHERE date = NOW()";
    $check_today_res = mysql_query($check_today_sql, $dbConn2);
    if (mysql_num_rows($check_today_res) > 0) {
    	echo "Random already been set for today";	
       	$row = mysql_fetch_assoc($check_today_res);
    	do {
    	$ids_today[] = $row['todays_id'];
    	} while ($row = mysql_fetch_assoc($check_today_res));
    
    	 $sql = "SELECT * FROM images WHERE id IN (";
    	foreach($ids_today as $id_today )
    	{
    	   $sql .= ",$id_today";
    	  } 
    	  $sql .= ")";
    	  //$sql = "SELECT * FROM images WHERE id IN(8,10,4)";
    	  //echo $sql;	
    	  $rsImages = mysql_query($sql, $dbConn2) or die(mysql_error());
    	$row_rsImages = mysql_fetch_assoc($rsImages); 
    
    Code (markup):
    I just need the
    
    $sql = "SELECT * FROM images WHERE id IN (";
    	foreach($ids_today as $id_today )
    	{
    	   $sql .= ",$id_today";
    	  } 
    	  $sql .= ")";
    
    Code (markup):
    part to work it gives me IN(,8,10,4)

    With your solution why the serialized random image array? serialize and unserialize part is not clear to me.

    grtz
     
    123GoToAndPlay, Mar 18, 2006 IP
  4. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ok with implode I am there
    
    $tmp_ids = array();
    	 $sql = "SELECT * FROM images WHERE id IN (";
    	foreach($ids_today as $id_today )
    	{
    	   $tmps_ids[] = $id_today;
    	  } 
    	  $sql .= implode(",", $tmps_ids);
    	  $sql .= ")";
    
    	  echo $sql;	
    
    Code (markup):
    Not very elegant but that will be next I guess.
     
    123GoToAndPlay, Mar 18, 2006 IP
  5. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    ok rewritten it to
    
    $tmp_ids = array();
        foreach($ids_today as $id_today )
        {
           $tmps_ids[] = $id_today;
      }
         
          $sql = "SELECT * FROM images WHERE id IN (".implode(",", $tmps_ids).")";
    
    Code (markup):
     
    123GoToAndPlay, Mar 18, 2006 IP
  6. rvarcher

    rvarcher Peon

    Messages:
    69
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Ok, Create a new table in your db.

    Let's say it's called todays_random_images

    It has 2 fields. id | pic_data

    create 1 record in the table where the id = 1. There's no need to put anything in the data field yet.

    There will only be one record for this table.

    serialize and unserialize are php commands that deal with preparing arrays for storing in a db.

    (although the idea is present in many languages)

    they are talked about indepth on php.net


    Here's the code:

    Note: I have not tested this. Just wrote it from scratch.

    
    <?php
    
         //////////  define your table name here to minimize updates later if table name changes.
    
         $pic_table = "todays_random_images";
    
    
         //////////  get the data about your random pictures from your table
    
         $query = "SELECT pic_data FROM $pic_table WHERE id = '1'";
    
         $row = mysql_fetch_assoc(mysql_query($query));
    
    
         //////////  Turn the pic_data back into an array
    
         if ( !empty($row['pic_data']) ) $random_pic_arr = unserialize ( $row['pic_data'] );
    
    
         if ( empty($random_pic_arr) ) $random_pic_arr = array();
    
    
         /////////  Check if today's day is different from day in $random_pic_arr
         /////////  If yes, get new random pics and update table with new data
    
         /////////  If no, we're good, no need to change the pic ids already defined in your array.
    
         $todays_day = date("j");
    
    
         if ( $random_pic_arr['date'] != $todays_day )
         {
    
              //////////  Must update the date in the array.
    
              $random_pic_arr['date'] = $todays_day;
    
    
              //////////  Get three new random picture ids.
    
              //////////  Keep in mind your 'id' field might be named differently
    
              $query = "SELECT id FROM images ORDER BY rand() LIMIT 3";
    
              $result = mysql_query($query);
    
              while ( $row = mysql_fetch_assoc($result) )
              {
                   $x++;
    
                   $random_pic_arr["$x"] = $row['id'];
              }
    
              //////////  Now prepare array so we can update the table and update table.
    
              $pic_update_data = serialize ( $random_pic_arr );
    
              //////////  addSlashes to data so it is safe to stick into the table
    
              if ( !get_magic_quotes_gpc() )
              {
                   $pic_update_data = addslashes ( $pic_update_data );
              }
    
              Update table with new data
    
              $query = "UPDATE $pic_table SET pic_data = '$pic_update_data' WHERE id = '1'";
    
              mysql_query($query);
    
         }
    
    
         //////////  At this point we have:
         //////////      $random_pic_arr['1']
         //////////      $random_pic_arr['2']
         //////////      $random_pic_arr['3']
         //////////  which contain the id's of the 3 pic to display.
    
         //////////  You can now retrieve the info about these 3 ids
         //////////  from your images table and display the images.
    
    
    ?>
    
    
    PHP:
     
    rvarcher, Mar 20, 2006 IP
  7. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    oh man, tx for your time and code.

    going to give it a shoot.
    grtz
     
    123GoToAndPlay, Mar 21, 2006 IP