Jump to content

Programming Logic Question


Recommended Posts

(This question might need to go to a different forum, because it is more of a general programming logic question than a specific PHP question, but I don't know where else to ask.  I am writing the program in PHP, so this is the first place I thought of to ask.  If I need to move this question somewhere else, please advise.  :confused: Thanks.)


The short version (straight to the point):

I need to generate a list of non-sequential integers and then randomize that list.



The longer version:  

I am writing a Bible quiz program and the first page has an HTML form where the user selects a question category (i.e. a book from the Bible.)  Once that selection is made, Each question in the MySQL database has an auto-increment questionID (integer) and a categoryname (string) as well as a question and an answer and some other fields.


I will need to gather a list of QuestionIDs corresponding to the chosen category, then ask the questions from that category, one-at-a-time--preferably in a random order.


The QuestionID is auto-incremented and new questions from 66 categories (66 books in the Bible) will be added daily.


I know how to use the rand() function on sequential integers, but this list will not be sequential.  Perhaps when I run the for each loop, I can add some code to stuff an array with the QuestionID for each of these questions.  I could then randomize that array, put that array into a session variable, and then ask questions from that array of QuestionIDs.  Does that sound like a good solution?   That solution seems easier to ask than to code, but before attempting to tackle that idea, I thought it would be better to run it by some "PHP Freaks" first.  Better ideas out there?  Thanks.

Link to comment
Share on other sites

$sql = 'SELECT QuestionId FROM Questions WHERE CategoryId = ?';
// prepare(), execute(), ...
$questionIds = $statement->fetchAll(PDO::FETCH_COLUMN, 0);

// store in session
$_SESSION['question_ids'] = $questionIds;
$_SESSION['question_pos'] = 0;
Link to comment
Share on other sites


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.