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条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题