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

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.