php中的分页限制数据但不在下一页显示
Posted
技术标签:
【中文标题】php中的分页限制数据但不在下一页显示【英文标题】:pagination in php limiting data but not displaying in the next page 【发布时间】:2019-12-28 06:50:49 【问题描述】:我有一个列出数据库数据的php网站,我的主页上有一个搜索按钮,当用户搜索关键字时,用户会被带到这个显示数据的页面。我已经完成了以下代码来列出数据并进行分页
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Search Result</title>
</head>
<body style="height:100%; width:100%">
<header>
<div class="header">
<h2>LOGO</h2>
</div>
<div id="navbar">
<a class="active" href="main.php">Home</a>
<a href="javascript:void(0)">News</a>
<a href="javascript:void(0)">Contact</a>
</div>
</header>
<?php
$limit = 8;
if (isset($_GET["page"])) $page = $_GET["page"]; else $page=1; ;
$start_from = ($page-1) * $limit;
$link = mysqli_connect("localhost", "root", "", "sample");
if($link === false)
die("ERROR: Could not connect. " . mysqli_connect_error());
if(isset($_REQUEST["search"]))
$sql = "SELECT * FROM image_gallery WHERE keyq LIKE ? OR keyw LIKE ? OR keye LIKE ? OR keyr LIKE ? OR keyt LIKE ? OR keyy LIKE ? OR keyu LIKE ? OR keyi LIKE ? OR keyo LIKE ? OR keyp LIKE ? LIMIT $start_from, $limit ";
if($stmt = mysqli_prepare($link, $sql))
mysqli_stmt_bind_param($stmt, "ssssssssss", $param_term, $king, $me, $you, $gig, $mig, $mug, $sug, $kez, $peb);
$param_term = $_REQUEST["search"] . '%';
$king = $_REQUEST["search"] . '%';
$me = $_REQUEST["search"] . '%';
$you = $_REQUEST["search"] . '%';
$gig = $_REQUEST["search"] . '%';
$mig = $_REQUEST["search"] . '%';
$mug = $_REQUEST["search"] . '%';
$sug = $_REQUEST["search"] . '%';
$kez = $_REQUEST["search"] . '%';
$peb = $_REQUEST["search"] . '%';
if(mysqli_stmt_execute($stmt))
$result = mysqli_stmt_get_result($stmt);
if(mysqli_num_rows($result) > 0)
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))?>
<div class="aamir"><span><img style="height:40px; width:55px; " class="img-responsive" src="admin/uploads/<?php echo $row['image'] ?>" /></span><small><?php echo $row['title']; ?></small><strong style="width:40%; height: 35%;"><em class="icon icon-chevron-down"></em><p style="margin-top: -5%;"> <?php echo $row['description']; ?> </p></strong>
<a
class="zayan" target="_blank" href="<?php echo $row['url']; ?>">VISIT</a>
</div>
<?php ?>
<?php
$results = mysqli_query($link,"SELECT COUNT(id) FROM image_gallery");
$row = mysqli_fetch_row($results);
$total_records = $row[0];
$total_pages = ceil($total_records / $limit);
$pagLink = "<div class='pagination'>";
for ($i=1; $i<=$total_pages; $i++)
$pagLink .= "<a class='circle' href='view.php?page=".$i."'>".$i."</a>";
;
echo $pagLink . "</div>";
?>
<?php
else
echo "<p>No matches found <br> <a href='index.php'>Go Back</a></p>";
else
echo "ERROR " . mysqli_error($link);
// Close statement
mysqli_stmt_close($stmt);
// close connection
mysqli_close($link);
?>
</body>
</html>
(注意:我给出了相同的关键字,所以当我搜索该关键字时,每个数据都会显示)数据被限制得很好,但剩余的数据不会显示在分页的下一页中。谁能告诉我我的代码有什么问题?
【问题讨论】:
【参考方案1】:尝试在查询中切换 limit 和 start_from
$sql = "SELECT * FROM image_gallery WHERE keyq LIKE ? OR keyw LIKE ? OR keye LIKE ? OR keyr LIKE ? OR keyt LIKE ? OR keyy LIKE ? OR keyu LIKE ? OR keyi LIKE ? OR keyo LIKE ? OR keyp LIKE ? LIMIT $limit OFFSET $start_from";
【讨论】:
以上是关于php中的分页限制数据但不在下一页显示的主要内容,如果未能解决你的问题,请参考以下文章