PHP-使用for循环自动显示序列号 美国号 电影名称 语言
我想在表中自动显示获取结果的序列号。此处,序列号正确显示前五个结果,如1、2、3、4、5行 但在第二页上,数字显示为8,9,10,6,7 我在哪里犯错?我甚至尝试了while循环和forloop增量计数器。Im使用引导数据表显示数据库的结果。PHP-使用for循环自动显示序列号 美国号 电影名称 语言,php,Php,我想在表中自动显示获取结果的序列号。此处,序列号正确显示前五个结果,如1、2、3、4、5行 但在第二页上,数字显示为8,9,10,6,7 我在哪里犯错?我甚至尝试了while循环和forloop增量计数器。Im使用引导数据表显示数据库的结果。 <thead> <tr> <th> S.No </th> <th> Movie Name </th> <th> Langu
<thead>
<tr>
<th> S.No </th>
<th> Movie Name </th>
<th> Language </th>
</tr>
</thead>
<?php
$result = mysqli_query($connect,"SELECT * FROM movies ;")or die(mysqli_error());
$rows = mysqli_fetch_array($result);
?>
<tbody>
<?php
$counter=0;
foreach($result as $rows)
{
$counter=$counter+1;
?>
<?php echo "<tr><td>" . $counter . "</td><td>" . $rows['language'] . "</td><td>" . $rows['movie_name'] . "</td></tr>"; ?>
<?php } ?>
</tbody>
这应该对你有用。您需要在循环中获取结果才能解析它们。试试这个 必须在while循环外部声明
$i=1
,在while循环内部声明$i++
。因此,它将显示数据库中可用的行数。
您必须使用foreach的while循环instated
<?php
$result = mysqli_query($connect,"SELECT * FROM movies");
$counter=0;
while($rows = mysqli_fetch_array($result)){
echo "<tr><td>" . $counter . "</td>
<td>" . $result[$counter]['language'] . "</td>
<td>" . $result[$counter]['movie_name'] . "</td>
</tr>";
$counter++;
}
?>
美国号
电影名称
语言
您的foreach应该循环通过$rows
,而不是$result
。并且mysqli\u fetch\u数组
一次只返回一行。你需要一个循环。还有,你在这里展示的代码,以及你所说的你所看到的。。。不要对齐。我得到了类似“注意:未定义变量:I”的错误。我应该在while循环中使用forloop吗?我的坏消息是我想说$counter
,嵌套循环不是最好的选择,但如果它对你的大学项目有效,为什么不呢。但请记住,如果您可以避免嵌套循环,请避免它们。
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM movies";
$result = mysqli_query($conn, $sql);
//mysqli_close($conn);
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<table>
<thead>
<tr>
<th> S.No </th>
<th> Movie Name </th>
<th> Language </th>
</tr>
</thead>
<tbody>
<?php
$i=1;
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($rows = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>" . $i . "</td>
<td>" . $rows['language'] . "</td>
<td>" . $rows['movie_name'] . "</td>
</tr>";
$i++;
}
} else {
echo "0 results";
}
?>
</tbody>
</table>
</body>
</html>