rghollenbeck Posted October 31, 2013 Share Posted October 31, 2013 (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. 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 https://forums.phpfreaks.com/topic/283456-programming-logic-question/ Share on other sites More sharing options...
ignace Posted October 31, 2013 Share Posted October 31, 2013 $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; Link to comment https://forums.phpfreaks.com/topic/283456-programming-logic-question/#findComment-1456310 Share on other sites More sharing options...
rghollenbeck Posted October 31, 2013 Author Share Posted October 31, 2013 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. Link to comment https://forums.phpfreaks.com/topic/283456-programming-logic-question/#findComment-1456330 Share on other sites More sharing options...
AbraCadaver Posted October 31, 2013 Share Posted October 31, 2013 Why not just: SELECT QuestionId FROM Questions WHERE CategoryId = ? ORDER BY RAND() Link to comment https://forums.phpfreaks.com/topic/283456-programming-logic-question/#findComment-1456387 Share on other sites More sharing options...
rghollenbeck Posted October 31, 2013 Author Share Posted October 31, 2013 Thanks, AbraCadaver. Good point. I don't have to randomize the record numbers; randomizing the query results accomplishes the same thing. Link to comment https://forums.phpfreaks.com/topic/283456-programming-logic-question/#findComment-1456401 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.