dougui5419 2013-12-09 14:35
浏览 45
已采纳

增量在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

  • 写回答

2条回答 默认 最新

  • doubianyan9749 2013-12-09 14:38
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!