2017-07-15 33 views
0

我正在創建一個與http://www.sqlquiz.com/相同的測驗系統,並且在結果頁面中有問題。 如何將數據庫記錄一個接一個地發送到另一個頁面php

我已經建立4頁

的index.php ----> welcomePage其中i使用varible n的查詢字符串

的index.php

<p><a href="quizmain.php?n=1">Start SQL Quiz</a></p> 

2.quizmain.php- ----------->此頁面顯示問題及其選項,並在 獲得每個問題的響應後,它將進入計算得分的process.php頁面,每當計數器增加1以便在達到第10個問題後,最終結果頁面將顯示

quizmain.php

<?php 
    session_start(); 
    require_once("connection.php"); 
    extract($_REQUEST); 

    $number = (int) $_GET['n']; //starting value 1 
    echo $number; 
    $n1=rand(1,100); 

    $_SESSION['RQuestionNumber']=$n1; 

    $q=mysql_query("select * from quiz WHERE qno = '".$n1."'"); 

    $a=mysql_fetch_array($q); 


    echo $a['qno']; 
    echo $a['ans']; 

    ?> 




<body> 
    <table> 
    <tr> 
     <td width="757" height="390"><div align="center"> 


      <form method="post" action="process1.php"> 

       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
       <input type="radio" name="question" value="a" /> 
       <?php echo $a[2]?></label> 
      </p> 
     <p><br /> 
       <label> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
       <input type="radio" name="question" value="b" /> 
       <?php echo $a[3]?></label> 
      </p> 
     <p><br /> 
       <label> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
       <input type="radio" name="question" value="c" /> 
       <?php echo $a[4]?></label> 
      </p> 
     <p><br /> 
       <label> </label> 
       <label> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
       <input type="radio" name="question" value="d" /> 
       <?php echo $a[5]?></label> 
      </p> 

     <p>&nbsp; </p> 
     <p> 
      <label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      <input type="submit" value="Submit" name="Submit" /> 
      <input type="hidden" name="number" value="<?php echo $number; ?>" /> 
      </form> 
      </label> 
      <br /> 
      <br /> 
      <br /> 
     </p></td> 
    </tr> 
    </table> 
</body> 
</html> 

process.php

<?php 
    session_start(); 
    require_once('connection.php'); 
    extract($_REQUEST); 
     //Check to see if score is set_error_handler 
     if(!isset($_SESSION['score'])){ 
      $_SESSION['score'] = 0; 
     } 

     if($_POST) 
     { 
      $number = $_REQUEST['number']; //value of number is 1 initially 
      $selected_choice = $_REQUEST['question']; 
      $next = $number+1; // 

     $total=10; 
     $_SESSION['RQuestionNumber']; 


     $q = mysql_query("SELECT ans FROM quiz WHERE qno = '".$_SESSION['RQuestionNumber']."'"); 
     $result=mysql_fetch_array($q); 

     //$store=array(); 



      //Compare 
      if($result[0][0] == $selected_choice){ 
       //Answer is correct 
       $_SESSION['score']++; 
      } 


      //Check if last question 
      if($number == $total){ 
       header("location: resultTable.php"); 
       exit(); 
      } else { 
       header("location: quizmain.php?n=".$next); //now the value of n is 2 
      } 
     } 
     ?> 

resultTable.php

現在

在此頁我要打印所有顯示的問題在測驗中(同一組問題)以及m arked答案和正確答案 我已經嘗試使用會話變量,但它不工作。

<?php session_start(); ?> 
      <p>Final Score: <?php echo $_SESSION['score']; ?></p> 

<?php session_destroy(); ?> 
+0

行'$ _SESSION [ 'RQuestionNumber'];''中凝固酶原ss.php'看起來不正確 - 沒有設置值 - 如果不是'$ _SESSION ['RQuestionNumber'] = $ number;'? – RamRaider

回答

0

您的會話值將被覆蓋每次,所以最後你會得到最後的詢問id。如果你想要所有的問題ID然後使用數組。試試這個:

quizmain.php

<?php 
    session_start(); 
    require_once("connection.php"); 
    extract($_REQUEST); 

    $number = (int) $_GET['n']; //starting value 1 
    echo $number; 
    $n1=rand(1,100); 

    $_SESSION['RQuestionNumber'][]=$n1; 

    $q=mysql_query("select * from quiz WHERE qno = '".$n1."'"); 

    $a=mysql_fetch_array($q); 


    echo $a['qno']; 
    echo $a['ans']; 

    ?> 




<body> 
    <table> 
    <tr> 
     <td width="757" height="390"><div align="center"> 


      <form method="post" action="process1.php"> 

       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
       <input type="radio" name="question" value="a" /> 
       <?php echo $a[2]?></label> 
      </p> 
     <p><br /> 
       <label> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
       <input type="radio" name="question" value="b" /> 
       <?php echo $a[3]?></label> 
      </p> 
     <p><br /> 
       <label> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
       <input type="radio" name="question" value="c" /> 
       <?php echo $a[4]?></label> 
      </p> 
     <p><br /> 
       <label> </label> 
       <label> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
       <input type="radio" name="question" value="d" /> 
       <?php echo $a[5]?></label> 
      </p> 

     <p>&nbsp; </p> 
     <p> 
      <label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      <input type="submit" value="Submit" name="Submit" /> 
      <input type="hidden" name="number" value="<?php echo $number; ?>" /> 
      </form> 
      </label> 
      <br /> 
      <br /> 
      <br /> 
     </p></td> 
    </tr> 
    </table> 
</body> 
</html> 

process.php

<?php 
    session_start(); 
    require_once('connection.php'); 
    extract($_REQUEST); 
     //Check to see if score is set_error_handler 
     if(!isset($_SESSION['score'])){ 
      $_SESSION['score'] = 0; 
     } 

     if($_POST) 
     { 
      $number = $_REQUEST['number']; //value of number is 1 initially 
      $selected_choice = $_REQUEST['question']; 
      $next = $number+1; // 

     $total=10; 
     $last_que = $_SESSION['RQuestionNumber'][count($_SESSION['RQuestionNumber'])-1]; 
     // $_SESSION['RQuestionNumber']; 


     $q = mysql_query("SELECT ans FROM quiz WHERE qno = '".$last_que."'"); 
     $result=mysql_fetch_array($q); 

     //$store=array(); 



      //Compare 
      if($result[0][0] == $selected_choice){ 
       //Answer is correct 
       $_SESSION['score']++; 
      } 


      //Check if last question 
      if($number == $total){ 
       header("location: resultTable.php"); 
       exit(); 
      } else { 
       header("location: quizmain.php?n=".$next); //now the value of n is 2 
      } 
     } 
     ?> 

resultTable.php

<?php session_start(); ?> 
      <p>Final Score: <?php echo $_SESSION['score']; ?></p> 
      <?php 
      var_dump($_SESSION['RQuestionNumber']); //You will get all question number here 
      ?> 

<?php session_destroy(); ?> 
0

您CA n將數組存儲在會話中,所以每次運行process.php時,都可以添加一個帶有問題編號和給定答案的數組,這樣,您可以在resultTable.php中選擇數據庫中的10個問題,並與給定的答案進行比較。

添加到process.php:

if(!isset($_SESSION['answers'])) { 
    $answers = new array(); 
} else { 
    $answers = $_SESSION['answers']; 
} 

$answers[] = array($_SESSION['RQuestionNumber'], $selected_choice); 

$_SESSION['answers'] = $answers; 

那麼這段代碼的作用是創建一個數組存儲問題和用戶給出的答案,並在會話中保存它,這樣你可以循環對resultTable這個數組並在測驗中顯示所有問題。 PS:這是非常基本的代碼,爲了在測驗遊戲中使用它,你需要做很多事情,即:你應該檢查數組沒有從之前的運行

設置
+0

先生我得到的問題數字和用戶給出的所有問題的答案,但我怎麼才能得到的問題和他們正確的選擇使用這個信息從數據庫 –

0

您可以使用會話的會話的那
簡單的語法是:
session_start(); $_SESSION['score'] = $data; 現在你可以就這樣開始會議在另一頁使用$數據
session_start(); echo $_SESSION['score'];

要刪除數據會毀壞會議

session_destroy(); 
相關問題