增量在php while循环中

增量在php while循环中

问题描述:

Hi Guys I was wondering if you could help me with incrementing within my while loop. I am programming an online quiz using php and want the qustion number to update every time they select the submit button but the variable $questionNumber only stays at one

My code is shown below

<?php session_start(); ?>
<html>
<head>
<title> World Cup Quiz  </title>
</head>
<body>
<div align = center><strong> World Cup Quiz</strong></div>
<br />

<div align =center>
<?php



include ("dbConnect.php");

$_SESSION['number']=1;
$questionNumber = $_SESSION['number'];
$userScore=0;
$number= rand(1,4);

//search database for generated number and match ID
$dbQuery= "SELECT * FROM `questions 1.0` WHERE  `ID` =$number";
$dbResult=mysql_query($dbQuery);

echo "Question:".$questionNumber."/5<br>";

//Assign variables to each attribute

while ($dbRow=mysql_fetch_array($dbResult))

{


   $theID=$dbRow["ID"];
   $theQuestion=$dbRow["Question"];
   $theAnswer1=$dbRow["Correct Answer"];
   $theAnswer2=$dbRow["Wrong Answer 1"];
   $theAnswer3=$dbRow["Wrong Answer 2"];
   $theAnswer4=$dbRow["Wrong Answer 3"];
   $_SESSION['number']=$questionNumber+1;
}




  //Print Questions and Answers


    echo '<strong>'."$theQuestion".'</strong><br>';
   ?> <form name="correctAnswer" form method="post" action="quiz.php"> 
  <?php
   echo "$theAnswer1";?> <input type="radio" name="correctAnswer">
  <?php
   echo "<br>$theAnswer2"; ?> <input type="radio" name="wrongAnswer1"> 
   <?php
   echo "<br>$theAnswer3"; ?> <input type="radio" name="wrongAnswer2"> 
   <?php
   echo "<br>$theAnswer4"; ?> <input type="radio" name="wrongAnswer3"> 
   <br><input type="submit" value="Submit Answer">
   </form>


</div>
</body>

</html>

Hope you can help

Thanks

嗨大家好我想知道你是否可以帮助我在while循环中递增。 我正在使用php编写在线测验,并希望每次选择提交按钮时更新qustion编号,但变量$ questionNumber只保留在一个 p>

我的代码如下所示 p >

 &lt;?php session_start();  ?&GT; 
&LT; HTML&GT; 
&LT; HEAD&GT; 
&LT;标题&GT; 世界杯测验&lt; / title&gt; 
&lt; / head&gt; 
&lt; body&gt; 
&lt; div align = center&gt;&lt; strong&gt; 世界杯测验&lt; / strong&gt;&lt; / div&gt; 
&lt; br /&gt; 
 
&lt; div align = center&gt; 
&lt;?php 
 
 
 
include(“dbConnect.php”);  
 
 $ _SESSION ['number'] = 1; 
 $ questionNumber = $ _SESSION ['number']; 
 $ userScore = 0; 
 $ number = rand(1,4); 
 
  //搜索数据库以生成数字并匹配ID 
 $ dbQuery =“SELECT * FROM` questions 1.0` WHERE`ID` = $ number”; 
 $ dbResult = mysql_query($ dbQuery); 
 
echo“问题:  “。$ questionNumber。”/ 5&lt; br&gt;“; 
 
 //为每个属性分配变量
 
而($ dbRow = mysql_fetch_array($ dbResult))
 
 {
 
 
 $  theID = $ dbRow [“ID”]; 
 $ theQuestion = $ dbRow [“Question”]; 
 $ theAnswer1 = $ dbRow [“正确答案”]; 
 $ theAnswer2 = $ dbRow [“错误答案1”  ]; 
 $ theAnswer3 = $ dbRow [“错误答案2”]; 
 $ theAnswer4 = $ dbRow [“错误答案3”]; 
 $ _SESSION ['number'] = $ questionNumber + 1; 
}  
 
 
 
 
 //打印问题和答案
 
 
 echo'&lt; strong&gt;'。“$ theQuestion”。&lt; / strong&gt;&lt; br&gt;'; 
?  &GT;  &lt; form name =“correctAnswer”form method =“post”action =“quiz.php”&gt;  
&lt;?php 
 echo“$ theAnswer1”;?&gt;  &lt; input type =“radio”name =“correctAnswer”&gt; 
&lt;?php 
 echo“&lt; br&gt; $ theAnswer2”;  ?&GT;  &lt; input type =“radio”name =“wrongAnswer1”&gt;  
&lt;?php 
 echo“&lt; br&gt; $ theAnswer3”;  ?&GT;  &lt; input type =“radio”name =“wrongAnswer2”&gt;  
&lt;?php 
 echo“&lt; br&gt; $ theAnswer4”;  ?&GT;  &lt; input type =“radio”name =“wrongAnswer3”&gt;  
&lt; br&gt;&lt; input type =“submit”value =“提交答案”&gt; 
&lt; / form&gt; 
 
 
&lt; / div&gt; 
&lt; / body&gt; 
 
&lt;  / html&gt; 
  code>  pre> 
 
 

希望你能提供帮助 p>

谢谢 p> div>

You are resetting the number at the start of your script:

$_SESSION['number']=1;

You need to change that to something like:

if (!isset($_SESSION['number']))
{
    $_SESSION['number']=1;
}

You may change this part of your code to the following.

instead of : `

$_SESSION['number']=1;
$questionNumber = $_SESSION['number'];
$userScore=0;
$number= rand(1,4); 

write this :-

  $number= rand(1,4);
    $questionNumber = 0;
    if(isset($_SESSION['number']))
    {
        $_SESSION['number'] = $_SESSION['number'] + 1;
        $questionNumber = $_SESSION['number'];
    }else{
    $_SESSION['number'] = 1;
}

I think the problem is you are assigning the session to be equal to 1 before checking if it already set and valid session.

Hope that adds some