PHP递增? [英] PHP Incrementing?

查看:71
本文介绍了PHP递增?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一些PHP代码:

I have some PHP code here:

$a = 1;

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)每次显示都增加(这是MySQL数据库搜索的结果).例如1、2、3、4等

I want a number ($a) to increment each time it is displayed (it is the results from a MySQL database search). Such as 1, 2, 3, 4, etc.

推荐答案

您实际上没有PHP代码.您有HTML,PHP已将其回显.我说这并不是很繁琐,但可以帮助您了解为什么这不是您需要编辑的代码(或者至少不是全部代码).

You don't really have PHP code there. You have HTML which is being echoed out by PHP. I say this not to be pedantic, but to help you understand why this isn't the code you need to edit (or at least, not the whole of it).

听起来您正在寻找一个for循环.我猜您在粘贴的代码上方已经有一个代码了,但是如果没有,则需要将其添加到其中.您最终要寻找的代码类型是:

It sounds like what you are looking for is a for loop. I'm guessing you already have one in somewhere above the code you pasted, but if not, you'll need to add it in. The type of code you are ultimately looking for is:

echo "<table><tr><th>Table Headers</th><th>Go Here</th></tr>";
$rowcount = 1;
foreach ($tablerows as $row) {
    echo "<tr><td>".$rowcount."</td><td>Other row data></td><td>go here</td></tr>";
    $rowcount++;
}
echo "</table>";

对于输出的每一行,这还将输出$rowcount的当前值,该值每次通过循环都会增加一个.

This will, for each row that you output, also output the current value of $rowcount which, each time through the loop, gets incremented by one.

这篇关于PHP递增?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆