SitePoint Sponsor |
|
User Tag List
Results 1 to 10 of 10
Thread: Problem with array
-
Mar 28, 2009, 00:55 #1
- Join Date
- Jul 2008
- Location
- India
- Posts
- 57
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Problem with array
I am doing a quiz site using php and mysql wherein i am retrieving question and answers randomly from the database.Since the questions are getting repeated,i have decided to get the id of all the questions in an array and check for repeatability but dont know how to implement this using php..can anyone help me?
-
Mar 28, 2009, 02:40 #2
What have you tried so far?
-
Mar 28, 2009, 06:28 #3
- Join Date
- Oct 2006
- Location
- France, deep rural.
- Posts
- 6,869
- Mentioned
- 17 Post(s)
- Tagged
- 1 Thread(s)
Do you mean how do you search in a PHP array after you have got them out of your database?
PHP Code:$qs = array(10,11,23); // question numbers
$tgt = 10; // current question number
if( in_array($tgt, $qs )) {
echo 'duplicate found';
}else{
// do something positive
}
-
Mar 28, 2009, 07:18 #4
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
I'd use something like this.
PHP Code:<?php
Session_Start();
if(!isset($_SESSION['CurrentQuestions'])) $_SESSION['CurrentQuestions'] = array();
//If a question/answer has been submitted, save it to the session
if(array_key_exists('Question', $_POST)){
$_SESSION['CurrentQuestions'][intval($_POST['Question'])] = $_POST['Answer'];
}
//Get the IDs of the questions asked so far...
$CurrentQuesionIDs = Array_Keys($_SESSION['CurrentQuestions']);
//We want to exclude these from the query, so this can be placed in MySQL's NOT IN function
$NotIn = (Count($CurrentQuestionIDs) < 1) ? '-1' : implode(', ', $CurrentQuestionIDs);
//assuming you're using MySQL_Query, and that there are questions within topics...
$Query = MySQL_Query('SELECT ID, Question FROM Quiz WHERE Topic = 1 AND ID NOT IN(' . $NotIn . ') ORDER BY RAND()');
if(MySQL_Num_Rows($Query) == 0){
//Quiz has finished!
}else{
//Output Question
}Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Mar 28, 2009, 07:27 #5
the best way to do is to retrieve the questions and store them into session variable.
for example, if the quiz presents 10 questions, then script retrieves 10 random questions from database and put them into session variable.
this way, you'll save a lot of queries on your database and effectively eliminates duplicates.
-
Mar 30, 2009, 00:49 #6
- Join Date
- Jul 2008
- Location
- India
- Posts
- 57
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi arkinstall,
Can u please explain me ur code.....Also,all my questions gets loaded in a single page...
Pls refer quizpage.com
-
Mar 30, 2009, 11:04 #7
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
You're loading them on a single page!?
Then why not just:
PHP Code:$Query = MySQL_Query('SELECT columns FROM table ORDER BY RAND() LIMIT 10');
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
-
Mar 30, 2009, 11:24 #8
- Join Date
- Mar 2009
- Posts
- 3
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
just printr($var);
and see thare what is going on with your array.
-
Mar 30, 2009, 22:08 #9
- Join Date
- Jul 2008
- Location
- India
- Posts
- 57
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi Arkinstall,
The questions get loaded in a single page but only single question at a time and on clicking the continue button,next question should appear..Also can u help me out to find the difference between the start time and end time of the quiz.?Last edited by divyac; Mar 30, 2009 at 22:48.
-
Mar 31, 2009, 06:21 #10
Bookmarks