Php 如何显示while循环计数器变量
在下面的代码中,从mysql表中提取数据,在表标题“Sr.No.”的第一列中,我想在表数据中显示循环计数器变量$No。 谁能说出正确的语法Php 如何显示while循环计数器变量,php,Php,在下面的代码中,从mysql表中提取数据,在表标题“Sr.No.”的第一列中,我想在表数据中显示循环计数器变量$No。 谁能说出正确的语法 <table id = "result" class="data-table"> <caption class="title"></caption> <thead> <tr> <th>Sr.
<table id = "result" class="data-table">
<caption class="title"></caption>
<thead>
<tr>
<th>Sr.No.</th>
<th>Student ID</th>
<th>Student Name</th>
<th>Marks Obtained</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
$total = 0;
while ($row = mysqli_fetch_array($query))
{
$stu = $row['stu_id'] == 0 ? '' : number_format($row['stu_id']);
echo '<tr>
<td>''</td>
<td>'.$row['student_id'].'</td>
<input type="hidden" name="student_id" value='.$row['student_id'].'>
<td>'.$row['student_name'].'</td>
<input type="hidden" name="student_name" value='.$row['student_name'].'>
<td>'."<div class='search-block clearfix'><input name='obtmarks' placeholder='' type='number'></div>".'</td>
</tr>';
$total += $row['stu_id'];
$no++;
}?>
</tbody>
</table>
高级专员:没有。
学生证
学名
获得的分数
您可以打印$no变量值:
<table id = "result" class="data-table">
<caption class="title"></caption>
<thead>
<tr>
<th>Sr.No.</th>
<th>Student ID</th>
<th>Student Name</th>
<th>Marks Obtained</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
$total = 0;
while ($row = mysqli_fetch_array($query))
{
$stu = $row['stu_id'] == 0 ? '' : number_format($row['stu_id']);
echo '<tr>
<td>'.$no.'</td>
<td>'.$row['student_id'].'</td>
<input type="hidden" name="student_id" value='.$row['student_id'].'>
<td>'.$row['student_name'].'</td>
<input type="hidden" name="student_name" value='.$row['student_name'].'>
<td>'."<div class='search-block clearfix'><input name='obtmarks' placeholder='' type='number'></div>".'</td>
</tr>';
$total += $row['stu_id'];
$no++;
}?>
</tbody>
</table>
高级专员:没有。
学生证
学名
获得的分数
您可以打印$no变量值:
<table id = "result" class="data-table">
<caption class="title"></caption>
<thead>
<tr>
<th>Sr.No.</th>
<th>Student ID</th>
<th>Student Name</th>
<th>Marks Obtained</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
$total = 0;
while ($row = mysqli_fetch_array($query))
{
$stu = $row['stu_id'] == 0 ? '' : number_format($row['stu_id']);
echo '<tr>
<td>'.$no.'</td>
<td>'.$row['student_id'].'</td>
<input type="hidden" name="student_id" value='.$row['student_id'].'>
<td>'.$row['student_name'].'</td>
<input type="hidden" name="student_name" value='.$row['student_name'].'>
<td>'."<div class='search-block clearfix'><input name='obtmarks' placeholder='' type='number'></div>".'</td>
</tr>';
$total += $row['stu_id'];
$no++;
}?>
</tbody>
</table>
高级专员:没有。
学生证
学名
获得的分数
@Akhtar,我知道你对PHP世界很陌生。您之前收到的所有答案都是正确的,但是我想在您的问题中提供一些小的风格建议,这些建议将帮助您编写好代码并自己发现最终的问题
更改:
- 一般变量移到顶部
- PHP不仅仅是打印HTML,而是打印内容
- 将所有新变量合并到一个块中(
和$total
)$stu
- 注释未使用的变量(可能您将在将来的开发中使用)
元素移动到input
(单元格之间不应存在代码)td
- 统一引号(
)和双引号(”
),带双引号的HTML,带引号的PHP”
- 使用更清晰的语法更改了while括号(您会发现它对长HTML很有用)
高级专员:没有。
学生证
学名
获得的分数
@Akhtar,我知道你对PHP世界很陌生。你之前收到的所有答案都是正确的,但我想在你的问题中提供一些小的风格建议,帮助你编写好代码并自己发现最终的问题
更改:
- 一般变量移到顶部
- PHP不仅仅是打印HTML,而是打印内容
- 将所有新变量合并到一个块中(
$total
和$stu
)
- 注释未使用的变量(可能您将在将来的开发中使用)
input
元素移动到td
(单元格之间不应存在代码)
- 统一引号(
”
)和双引号(”
),带双引号的HTML,带引号的PHP
- 使用更清晰的语法更改了while括号(您会发现它对长HTML很有用)
通过这些更改,如果您打算使用现代IDE,您将得到更好的建议,代码突出显示将对您有所帮助
新代码:
高级专员:没有。
学生证
学名
获得的分数
在循环中的第一个打印$no。“$不.“您定义了$no
,现在在某个地方使用它。任何你想让它出现的地方。在第一个
@Siva thanx alot中,concat$no.
。我已经试过很多次了,但是我很惊讶为什么它对我不起作用。但是,它现在工作正常..在循环的第一个中打印$no'$不.“您定义了$no
,现在在某个地方使用它。任何你想让它出现的地方。在第一个
@Siva thanx alot中,concat$no.
。我已经试过很多次了,但是我很惊讶为什么它对我不起作用。但是,它现在可以正常工作了。非常感谢分享这些信息。我将遵守上述规则。谢谢分享这些信息。我将遵守上述规则。