WorldDrknss Posted October 15, 2007 Share Posted October 15, 2007 I have created a php script that is called every 10 seconds using ajax, but what I want to do is stop the images from repeating twice in a row. Here is the code: <?php $featured = array(); $query = $db->sql_query("SELECT id FROM featured"); while($images = $db->sql_fetchrow($query)){ $featured[] = $images['id']; } $random = array_rand($featured); if($featured[$random] > 0){ $image_query = $db->sql_query("SELECT image, title, link FROM featured WHERE id='".$featured[$random]."'"); list($image, $title, $link) = $db->sql_fetchrow($image_query); echo("<a href=\"$link\" onclick=\"window.open(this.href); return false\"><img src=\"/images/featured/$image\" title=\"$title\" alt=\"$title\" /></a>"); } ?> Link to comment https://forums.phpfreaks.com/topic/73315-solved-stoping-repeats/ Share on other sites More sharing options...
micah1701 Posted October 15, 2007 Share Posted October 15, 2007 maybe use a session var to record the currenlty displayed image, then check it before displaying the next image? <?php session_start(); $featured = array(); $query = $db->sql_query("SELECT id FROM featured"); while($images = $db->sql_fetchrow($query)){ $featured[] = $images['id']; } $random = array_rand($featured); if($random == $_SESSION['current_value']){ $random = array_rand($featured); //try again }else{ $_SESSION['current_value'] = $random; //assign new val to session } if($featured[$random] > 0){ $image_query = $db->sql_query("SELECT image, title, link FROM featured WHERE id='".$featured[$random]."'"); list($image, $title, $link) = $db->sql_fetchrow($image_query); echo("<a href=\"$link\" onclick=\"window.open(this.href); return false\"><img src=\"/images/featured/$image\" title=\"$title\" alt=\"$title\" /></a>"); } ?> Link to comment https://forums.phpfreaks.com/topic/73315-solved-stoping-repeats/#findComment-369946 Share on other sites More sharing options...
ToonMariner Posted October 15, 2007 Share Posted October 15, 2007 you don't need a session - just report back the id of the image to your ajax response handler. Use that value and store it in a global javascritp variable. Pass the value in your ajax request (remember to give it a default value - 0 should be good) and use it in your query by `image_id` != " . $_GET['current_image_id_passed'] .................... have fun. Link to comment https://forums.phpfreaks.com/topic/73315-solved-stoping-repeats/#findComment-369952 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.