Jump to content

[SOLVED] Stoping repeats


WorldDrknss

Recommended Posts

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

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>");
}
?>

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.

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.