tags:

views:

27

answers:

1

Hi,

I have this code:

$result = mysql_query("SELECT * FROM quote ORDER BY RAND() LIMIT 1") or die(mysql_error());  

$row = mysql_fetch_array( $result );

echo $row['frase'];

It echo´s a random "frase" every time you query.

What I want to do is avoid having the same result in consecutive queries.

To make myself clear, if in my database I´ve got:

1   a
2   b
3   c

If from echo $row['frase']; I got a the the next result can´t be a.

I hope I made myself clear and if not please comment and I´ll expand!

Thanks in advance!

BTW: sorry to @deceze @zerkms @Dr.Molle because last question was a total mess! No harm ment, just a noob on a spree :)

EDIT:

To answer Jason McCreary I actually call it like this:

<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","ajax_info.php",true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>
</html>
+2  A: 

Since this last query needs to persist across page refreshes, you need to use a session. Something along these lines:

session_start();

if (empty($_SESSION['lastresult'])) {
    $_SESSION['lastresult'] = null;
}

$query = "SELECT * FROM `quote` WHERE `id` != '%s' ORDER BY RAND() LIMIT 1";
$query = sprintf($query, mysql_real_escape_string($_SESSION['lastresult']));

$result = mysql_query($query) or die(mysql_error());  

$row = mysql_fetch_array($result);

$_SESSION['lastresult'] = $row['id'];

echo $row['frase'];

You may want to store the last x results in the session and use NOT IN () in your query to avoid every second (third, forth, ...) quote being the same.

deceze
Thanks deceze I´m trying it out!
Trufa
Absolutely fantastic thank you very much!
Trufa