Jump to content

Random Row


savagenoob

Recommended Posts

I am using MySQL to return a random row but it loops through and I was wondering of a way to not return a row that has already been displayed. Its a quiz program and the user selects the number of questions they want displayed and the code loops through that many rows.

 

$select = "SELECT * FROM pq_crtp_quiz WHERE testname = '$testname' ORDER BY RAND() DESC LIMIT 1";

Link to comment
https://forums.phpfreaks.com/topic/155593-random-row/
Share on other sites

Then something like this perhaps (assuming id is the primary key):

 

session_start();

if (!isset($_SESSION['selected'] || !$in = join(',', $_SESSION['selected'])) {
$in = null;
}
else {
$in = ' AND id NOT IN (' . $in . ')';
}

$res = mysql_query("SELECT * FROM pq_crtp_quiz WHERE testname = '{$testname}'{$in} ORDER BY RAND() DESC LIMIT 1");

$row = mysql_fetch_assoc($res);

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

Link to comment
https://forums.phpfreaks.com/topic/155593-random-row/#findComment-818908
Share on other sites

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.