0

这是我的 PHP 脚本中的代码:

while($runrows = mysql_fetch_array($getquery)) //fetching results
{
$id = $runrows ['ID'];
$answer = $runrows ['Answer']; 
$category = $runrows ['Category']; 
$num = $runrows ['Question #']; 
$difficulty = $runrows ['Difficulty'];  
$question = $runrows ['Question']; 
$round = $runrows ['Round'];  
$tournament = $runrows ['Tournament'];  
$year = $runrows ['Year'];  
$a = 1;


//what will be displayed on the results page 
echo "
<div>$a</div>
<div class='alert alert-info' style='border-radius: 20px'>
<div style='padding: 10px; span5'>
<span class='label label-info' font-size='30px'><em>Tournament | Year | Round | Question # | Category</em></span><span style='margin-left: 500px; text-align: right'>ID: $id</span></div>
<b>$tournament |</b> <b>$year |</b> <b>$round |</b> <b>$num |</b> <b>$category</b>
<p><em>Question:</em> $question</p>
<div class='row'><div class='alert alert-info span7'><em><strong>ANSWER:</strong></em> $answer </div><div class='alert alert-info span2' align='right'>
<a href='#errorReportmodel' class='btn btn-inverse' data-toggle='modal'>Report an Error</a></div></div></div><hr>
";

}

我将如何编辑此代码,以便 $a 为每个结果增加 1。我希望数字“1”在结果 1 旁边,数字“2”在结果 2 旁边,等等。

4

2 回答 2

1

尝试$a在 while 循环之外初始化变量,这样就可以轻松地对其进行操作而无需重置:

$a = 1;
while($runrows = mysql_fetch_array($getquery)) //fetching results
{
$id = $runrows ['ID'];
$answer = $runrows ['Answer']; 
$category = $runrows ['Category']; 
$num = $runrows ['Question #']; 
$difficulty = $runrows ['Difficulty'];  
$question = $runrows ['Question']; 
$round = $runrows ['Round'];  
$tournament = $runrows ['Tournament'];  
$year = $runrows ['Year'];  



//what will be displayed on the results page 
echo "
<div>$a</div>
<div class='alert alert-info' style='border-radius: 20px'>
<div style='padding: 10px; span5'>
<span class='label label-info' font-size='30px'><em>Tournament | Year | Round | Question # | Category</em></span><span style='margin-left: 500px; text-align: right'>ID: $id</span></div>
<b>$tournament |</b> <b>$year |</b> <b>$round |</b> <b>$num |</b> <b>$category</b>
<p><em>Question:</em> $question</p>
<div class='row'><div class='alert alert-info span7'><em><strong>ANSWER:</strong></em> $answer </div><div class='alert alert-info span2' align='right'>
<a href='#errorReportmodel' class='btn btn-inverse' data-toggle='modal'>Report an Error</a></div></div></div><hr>
";

}

然后使用 将$a++前一个整数加一。一个示例用法是:

echo "
<div>$a</div>
<div class='alert alert-info' style='border-radius: 20px'>
<div style='padding: 10px; span5'>
<span class='label label-info' font-size='30px'><em>Tournament | Year | Round | Question # | Category</em></span><span style='margin-left: 500px; text-align: right'>ID: $id</span></div>
<b>$tournament |</b> <b>$year |</b> <b>$round |</b> <b>$num |</b> <b>$category</b>
<p><em>Question:</em> $question</p>
<div class='row'><div class='alert alert-info span7'><em><strong>ANSWER:</strong></em> $answer </div><div class='alert alert-info span2' align='right'>
<a href='#errorReportmodel' class='btn btn-inverse' data-toggle='modal'>Report an Error</a></div></div></div><hr>
";
  $a++; // Added here, so the contents of $a will be echoed prior to adding one. 
}
于 2013-06-30T02:53:17.920 回答
1
$a=1;
while($runrows = mysql_fetch_array($getquery)) {
//YOUR CODE
$a++;
}
于 2013-06-30T02:54:28.190 回答