这是我的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旁边等等。
答案 0 :(得分:1)
尝试在while循环外部初始化$a
变量,这样可以轻松操作而不会重置:
$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.
}
答案 1 :(得分:1)
$a=1;
while($runrows = mysql_fetch_array($getquery)) {
//YOUR CODE
$a++;
}