SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    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?

  2. #2
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,561
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What have you tried so far?

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    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


  4. #4
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    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

  5. #5
    SitePoint Enthusiast v1rgil's Avatar
    Join Date
    Dec 2008
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    SitePoint Enthusiast
    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

  7. #7
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    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'); 
    That will get questions in a random order, and they won't be repeated unless you have repeated rows in the table.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  8. #8
    SitePoint Member
    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.

  9. #9
    SitePoint Enthusiast
    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.

  10. #10
    SitePoint Enthusiast v1rgil's Avatar
    Join Date
    Dec 2008
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by divyac View Post
    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.?
    use Javascript date object to get the current time as the start time when page loads, then when user hits next get current time and subtract the start time. that's how you get the elapsed time.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •