Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Aug 2012
    Posts
    44
    Thanks
    26
    Thanked 0 Times in 0 Posts

    Store several words in a session array and then show them

    I'm writting a quiz. First the question is shown in question.php then in answer.php, the answer is analised, if the answer is right the word is stored in an array and if wrong in another. Then go back to question.php and so on. When there are no more questions, the result.php will show which words have been correct and which ones were wrong.

    Please note that the session array needs to include a new word every time the answer.php is loaded.

    My code:

    answer.php

    PHP Code:
     <?php
     session_start
    ();

     
    $question $_GET['question'];
     
    $user_answer $_GET['user_answer'];
     
    $right_answer mysql_query("SELECT french FROM words where english='$question'");
     
    $fila mysql_fetch_assoc($right_answer); 
     
    $fila_french $fila['french'];

     if ( 
    $user_answer == $fila_french ) { 
        echo 
    "Right"
        
    $_SESSION['right_words'] = array();
        
    array_push($_SESSION['right_words'], $question);
         }

         else { echo 
    "Wrong"
         
    $_SESSION['wrong_words'] = array();
        
    array_push($_SESSION['wrong_words'], $question);
         }
     echo 
    "<a href='question.php' > Next </a>";
     
    ?>
    result.php :

    PHP Code:
    <?php
     session_start
    ();

     
    $right_answers implode(',',$_SESSION['right_words']);
     
    $wrong_answers implode(',',$_SESSION['wrong_words']);  

     echo 
    "<h1> You finished the test </h1> 
     <p> You have these words right: $right_answers</p>
     <p> You have these words wrong: $wrong_answers</p>
     <a href='../exercises.php'> Go back to exercises </a>"
    ;
     
    ?>
    The problem is that the session array is only storing the first item because the assignment to array() wipes out anything that was there previously and creates a new empty array.

    Thanks for you help!
    Last edited by cast_no_shadow; 11-05-2012 at 07:32 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Yep, that's easily fixed:
    PHP Code:
    if (!isset($_SESSION['right_words']))
    {
        
    $_SESSION['right_words'] = array();
    }
    //. . .
    if (!isset($_SESSION['wrong_words']))
    {
        
    $_SESSION['wrong_words'] = array();

    Simply check if it exists before attempting to create it. If it does, then don't create it again.
    This can be done anytime before you intend to use the applicable array.

  • #3
    New Coder
    Join Date
    Aug 2012
    Posts
    44
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Yep, that's easily fixed:
    PHP Code:
    if (!isset($_SESSION['right_words']))
    {
        
    $_SESSION['right_words'] = array();
    }
    //. . .
    if (!isset($_SESSION['wrong_words']))
    {
        
    $_SESSION['wrong_words'] = array();

    Simply check if it exists before attempting to create it. If it does, then don't create it again.
    This can be done anytime before you intend to use the applicable array.
    You mean like this?:

    PHP Code:
         if ( $user_answer == $fila_french ) { echo "Right";
              
                  if (!isset(
    $_SESSION['right_words'])) 
                      { 
    $_SESSION['right_words'] = array();}
                  
                  else { 
    array_push($_SESSION['right_words'], $question);}

              }
              
              else { echo 
    "Wrong"
                      
                  if (!isset(
    $_SESSION['wrong_words'])) 
                      { 
    $_SESSION['wrong_words'] = array();}
                  
                  else { 
    array_push($_SESSION['wrong_words'], $question);}
              } 
    Last edited by cast_no_shadow; 11-05-2012 at 05:53 PM.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    No else statements. Just the if. You want to push onto it either way, you only want to initialize the array if it has not yet been initialized.

  • #5
    New Coder
    Join Date
    Aug 2012
    Posts
    44
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    No else statements. Just the if. You want to push onto it either way, you only want to initialize the array if it has not yet been initialized.
    Thanks for your answer, but it is not working, with or without the else's

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by cast_no_shadow View Post
    Thanks for your answer, but it is not working, with or without the else's
    Post the entire code you currently have.

  • #7
    New Coder
    Join Date
    Aug 2012
    Posts
    44
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Post the entire code you currently have.
    answer.php

    PHP Code:
    <?php

    session_start
    ();  
        
        
    // Contect to de DB
            
    include("config.php");
            
        
    $lesson_n $_SESSION['lesson_n'];
        
    $question $_GET['question'];
        
    $user_answer $_GET['user_answer'];
        
    $right_answer mysql_query("SELECT french FROM words where english='$question'");
        
    $fila mysql_fetch_assoc($right_answer); 
        
    $fila_french $fila['french'];
        
            
    // Get the audio
            
    $audio mysql_query("SELECT audio_name FROM words where english='$question'");
            
    $audio2 mysql_fetch_array($audio);
            
            
    // Know how many words (rows are in the lesson)
            
    $query mysql_query("SELECT * FROM words where lesson_number='$lesson_n'");
            
    $num_rows mysql_num_rows($query);
              
              echo 
    "<p> Lesson nº $lesson_n </p>";
              echo 
    "<p> Question $question </p>";
              
              
              
              if ( 
    $user_answer == $fila_french ) { echo "Right";
              
                if (!isset(
    $_SESSION['right_words'])) 
                      { 
    $_SESSION['right_words'] = array();}


              }
              
              else { echo 
    "Wrong"
                      
                if (!isset(
    $_SESSION['wrong_words'])) 
                      { 
    $_SESSION['wrong_words'] = array();}
                  
              }
              

              echo 
    "<p> Your answer was $user_answer </p>";
              echo 
    "<p> The right answer is $fila_french 
                          <a href='../audio/"
    .$audio2['audio_name'].".mp3' class='sm2_button'></a> 
                      </p>"
    ;
              
              echo 
    "<a href='question.php' > Next </a>";
        


    ?>
    and then question.php

    PHP Code:
    <?php 

    session_start
    (); 
        
    $lesson_n $_SESSION['lesson_n'];
            
            
    // Contect to de DB
            
    include("config.php");
            
            
    // Know how many words (rows) are in the lesson
            
    $query mysql_query("SELECT * FROM words where lesson_number='$lesson_n'");
            
    $num_rows mysql_num_rows($query);
            
            
    // Order of the words
            
    if(empty($_SESSION['count'])) $_SESSION['count'] = 0;
             
    $order =  $_SESSION['count']+1;
              
    $_SESSION['count'] =  $order
              
    $order_query $order-1
       
       
    // Print the word in french and in english
            
    $result mysql_query("SELECT * FROM words where lesson_number='$lesson_n' ORDER BY id LIMIT $order_query,1");
            
    $info2 mysql_fetch_array($result);
            
            if ( 
    $num_rows >= $order ) {

            echo 
    " <h1>Question</h1>
                     <p>Lesson nº $lesson_n </p>
                       <p> Word $order / $num_rows "
    ;
            
            echo 
    "<p>".$info2['english']."</p>";
            
            echo 
    "<form name='bank_words' action='answer.php' method='GET' id='theFormID'>
                <input type='hidden' name='question' value='"
    .$info2['english']."' />
                <input type='text' name='user_answer' autocomplete='off' id='theFieldID' autofocus='autofocus' autofocus />
                <script type='text/javascript'>theFormID.theFieldID.focus();</script>
         

                <input class='big_b' type='submit'  value='check the answer' />
                
            </form> "
    ;
            
             }
             
             
    // When there are no more words you finish the test and get the results
             
    else {
             
             
    $right_answers implode(',',$_SESSION['right_words']);
             
    $wrong_answers implode(',',$_SESSION['wrong_words']); 
     
             
             echo 
    "<h1> You finished the test </h1> 
                     <p> You have these words right: $right_answers</p>
                     <p> You have these words wrong: $wrong_answers</p>
                     
                     <a href='../exercises.php'> Go back to exercises </a>
             
             "
    ;}
        
        
        
    ?>
    Last edited by cast_no_shadow; 11-05-2012 at 06:19 PM.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You don't have the array_push in the answers.php. You've initialized the arrays only once, but you don't add anything to them.

  • Users who have thanked Fou-Lu for this post:

    cast_no_shadow (11-05-2012)


  •  

    Posting Permissions

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