我是关于php的新手,试图学习。 我已经搜索了关于while循环分页但不满意的其他主题。
我的数据库中有70条用户记录,希望用html表列出它们。我用这段代码显示所有记录。如何使用这些代码进行简单的分页?请帮帮我。
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Surname</th>
<th>Email</th>
<th>Password</th>
<th>Date</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
<?php
$q = "SELECT * FROM users ORDER BY uid ASC";
$r = mysqli_query($dbc,$q);
while($userlist = mysqli_fetch_assoc($r)){ ?>
<tr>
<td><?php echo $userlist['uid']; ?></td>
<td><?php echo $userlist['name']; ?></td>
<td><?php echo $userlist['surname']; ?></td>
<td><?php echo $userlist['email']; ?></td>
<td><?php echo $userlist['password']; ?></td>
<td><?php echo $userlist['date']; ?></td>
<td><?php echo $userlist['gender']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
答案 0 :(得分:2)
以下提供非常简单的分页作为起点。您需要为分页提供格式化等。
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Surname</th>
<th>Email</th>
<th>Password</th>
<th>Date</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
<?php
$q = "SELECT count(*) as `numrows` FROM `users` ORDER BY `uid` ASC";
$c = mysqli_query($dbc,$q);
if($c) {
if($t = mysqli_fetch_assoc($c)) {
$numrows = $t['numrows'];
}
}
$numrows = 0;
$rowsperpage = 10;
$currpage = isset($_REQUEST['currpageno']) && $_REQUEST['currpageno'] != 0 ? $_REQUEST['currpageno'] : 1;
$numpages = ceil($numrows / $rowsperpage);
$startrow = ($currpage - 1) * $rowsperpage;
if($startrow > $numrows) {
$startrow = $numrows - $rowsperpage;
}
if($startrow < 0) {
$startrow = 0;
}
$q = "SELECT * FROM `users` ORDER BY `uid` ASC LIMIT ".$startrow.",".$rowsperpage.";";
$r = mysqli_query($dbc,$q);
while($userlist = mysqli_fetch_assoc($r)){
?>
<tr>
<td><?php echo $userlist['uid']; ?></td>
<td><?php echo $userlist['name']; ?></td>
<td><?php echo $userlist['surname']; ?></td>
<td><?php echo $userlist['email']; ?></td>
<td><?php echo $userlist['password']; ?></td>
<td><?php echo $userlist['date']; ?></td>
<td><?php echo $userlist['gender']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<div id='pagination'>
<?php
for($pgno = 1;$pgno <= $numpages;$pgno++) {
echo "<a class='' href='?currpageno=".$pgno."'>".$pgno."</a>";
}
?>
</div>