Jump to content

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.

Edited by rghollenbeck
Link to comment
https://forums.phpfreaks.com/topic/283456-programming-logic-question/
Share on other sites

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

// store in session
$_SESSION['question_ids'] = $questionIds;
$_SESSION['question_pos'] = 0;

shuffle($questionIds);

 

How awesome!  This function had my problem in mind exactly.  I was pondering how to randomize non-sequential integers--an impossible task.  Shuffle() solves this problem nicely.

 

Thank you ignace.

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.