我的代码从属于特定用户的数据库获取值。
<?php
$user = $_SESSION['username'];
$q = intval($_GET['q']);
$db = mysqli_connect('localhost', 'username', 'password', 'database_name');
$sql = "SELECT * FROM savedtimes WHERE username = '$user' AND session = '$q' ORDER BY timeid";
$result = mysqli_query($db, $sql);
//$SNo = I might need to put something over here which makes it show the order number.
echo "<table>
<tr>
<th>S.No.</th>
<th>time</th>
<th>Avg of 5</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $SNo . "</td>";//This is the S.no column.
echo "<td>" . $row['value2'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
现在,虽然它按照名为timeid的字段的顺序显示这些值,这是一个自动递增的字段,我希望有一个名为S.No的列(仅在显示时,而不是在实际数据库中),哪个订单他们从1到最后一个号码。请注意,我不能只使用&time;&#39; timeid&#39;因为我有多个用户,所以这些数字不会是连续的。所以基本上,订单是&time;&#39; timeid&#39;,但我希望列显示连续数字。我该怎么做?
答案 0 :(得分:3)
在1
循环之外声明一个计数器变量(值为while()
)。在循环中显示它,然后在循环结束时递增它。
// your code
$SNo = 1;
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $SNo . "</td>";
echo "<td>" . $row['value2'] . "</td>";
echo "</tr>";
++$SNo;
}
// your code