Jump to content


Photo

Programming Logic Question

logic

  • Please log in to reply
4 replies to this topic

#1 rghollenbeck

rghollenbeck

    Advanced Member

  • Members
  • PipPipPip
  • 63 posts
  • LocationRiverside, California, USA

Posted 30 October 2013 - 08:50 PM

(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, 30 October 2013 - 08:51 PM.


#2 ignace

ignace

    Now mod flavored

  • Moderators
  • 6,247 posts
  • LocationBelgium

Posted 31 October 2013 - 01:50 AM

$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;


#3 rghollenbeck

rghollenbeck

    Advanced Member

  • Members
  • PipPipPip
  • 63 posts
  • LocationRiverside, California, USA

Posted 31 October 2013 - 05:43 AM

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.



#4 AbraCadaver

AbraCadaver

    Cracka Memba

  • Gurus
  • 1,888 posts
  • LocationThe Republic of Texas

Posted 31 October 2013 - 12:49 PM

Why not just:

SELECT QuestionId FROM Questions WHERE CategoryId = ? ORDER BY RAND()

mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.

#5 rghollenbeck

rghollenbeck

    Advanced Member

  • Members
  • PipPipPip
  • 63 posts
  • LocationRiverside, California, USA

Posted 31 October 2013 - 01:49 PM

Thanks, AbraCadaver.  Good point.  I don't have to randomize the record numbers; randomizing the query results accomplishes the same thing.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com