douao8353 2012-11-05 13:17
浏览 50
已采纳

将多个单词存储在会话数组中,然后显示它们

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.

EDIT: The problem is that the session array is only storing the first item

My code:

answer.php :

 <?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
 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>";
 ?>

Thanks for you help!

  • 写回答

1条回答 默认 最新

  • dongpi0658 2012-11-05 13:23
    关注

    You don't say what the problem is but you will only get 1 item in each array as in your if/else blocks you are assigning a new empty array to your session variables. You should check if these variables are already defined before doing this:

    if (!$_SESSION['right_words']) { $_SESSION['right_words'] = array(); }
    

    And do the same for 'wrong_words'.

    For your result.php file, to list all of the answers separated by a comma you can use implode:

    $right_answers = implode(',',$_SESSION['right_words']);
    $wrong_answers = implode(',',$_SESSION['wrong_words']);  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序