首页 > 解决方案 > 分页没有出现在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">&laquo;</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">&raquo;</span>
        <span class="sr-only">Last</span>
      </a>
    </li>
    <?php } ?>
  </ul>
</nav>
<?php }?>

数据已正确添加到数据库中,但问题是页面中看不到分页。当我只是把它来的 html,但是当我在其中添加 php 代码时,它没有来。谁能告诉我可能是什么问题?

标签: javascriptphphtmlsql

解决方案


<nav></nav>代码移到里面 if(mysqli_num_rows($result) > 0){ }

在您的餐桌之前或之后。

问题是您现在仅在您的if语句失败时才显示它:if($result = mysqli_query($link, $sql)){ } else { }

提示: 为了防止 SQL 注入,我建议:

PHP MySQLi Prepared Statements Tutorial

PHP PDO 准备语句教程


推荐阅读