javascript - 分页没有出现在php中
问题描述
我在php中有一个表格,其数据来自sql数据库,所以我为表格保留了分页,因为数据很长
<?php
require_once "config.php";
$perpage = 5;
if(isset($_GET['page']) & !empty($_GET['page'])){
$curpage = $_GET['page'];
}else{
$curpage = 1;
}
$start = ($curpage * $perpage) - $perpage;
$sql = "SELECT * FROM `registers` LIMIT $start, $perpage";
$PageSql = "SELECT * FROM `registers`";
$pageres = mysqli_query($link, $PageSql);
$totalres = mysqli_num_rows($pageres);
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table class='table'>";
echo "<thead>";
echo "<tr>";
echo "<th class='serial'>ID</th>";
echo "<th>Name</th>";
echo "<th>Phone Number</th>";
echo "<th>Email</th>";
echo "<th>Action</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>";
echo "<a href='read.php?id=". $row['id'] ."' title='View Record' data-toggle='tooltip'><span class='glyphicon glyphicon-eye-open'></span></a>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "<p class='lead'><em>No records were found.</em></p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
?>
<nav aria-label="Page navigation">
<ul class="pagination">
<?php if($curpage != $startpage){ ?>
<li class="page-item">
<a class="page-link" href="?page=<?php echo $startpage ?>" tabindex="-1" aria-label="Previous">
<span aria-hidden="true">«</span>
<span class="sr-only">First</span>
</a>
</li>
<?php } ?>
<?php if($curpage >= 2){ ?>
<li class="page-item"><a class="page-link" href="?page=<?php echo $previouspage ?>"><?php echo $previouspage ?></a></li>
<?php } ?>
<li class="page-item active"><a class="page-link" href="?page=<?php echo $curpage ?>"><?php echo $curpage ?></a></li>
<?php if($curpage != $endpage){ ?>
<li class="page-item"><a class="page-link" href="?page=<?php echo $nextpage ?>"><?php echo $nextpage ?></a></li>
<li class="page-item">
<a class="page-link" href="?page=<?php echo $endpage ?>" aria-label="Next">
<span aria-hidden="true">»</span>
<span class="sr-only">Last</span>
</a>
</li>
<?php } ?>
</ul>
</nav>
<?php }?>
数据已正确添加到数据库中,但问题是页面中看不到分页。当我只是把它来的 html,但是当我在其中添加 php 代码时,它没有来。谁能告诉我可能是什么问题?
解决方案
将<nav></nav>
代码移到里面
if(mysqli_num_rows($result) > 0){ }
在您的餐桌之前或之后。
问题是您现在仅在您的if
语句失败时才显示它:if($result = mysqli_query($link, $sql)){ } else { }
提示:
为了防止 SQL 注入,我建议:
PHP MySQLi Prepared Statements Tutorial
推荐阅读
- if-statement - OPL CPLEX If 语句与布尔值一起使用
- html - 杂散结束标签“表格”
- scala - 如何在 Scala 中测试 Spark 应用程序
- sql - 如何在 MS SQL 中获取上个月的最新日期
- flutter - 我怎么能带这个词?(扑)
- javascript - React Material-UI Select 使用对象数组作为源
- wso2 - WSO2 EI 6.6.0 我部署的 API 可以工作,但经过一段时间后它停止响应
- r - 将向量按列转换为数据框
- android - 在下拉状态下更改 Material TextInputLayout 笔触颜色
- django - 带有 AWS S3 的 Django 中的静态文件