Jump to content

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

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.