0
我有一些PHP和HTML代码从我的数据库加载结果。它显示每页五个结果。假设我有1000页。所有这些页面的链接都会从屏幕上消失。谷歌有这个问题,但他们通过仅显示当前链接以及5个链接和5个链接来修复它。我想要做这样的事情。我不想显示100个链接到各个页面。假装用户在第100页上。我想要显示第100页的链接以及95到105的链接。我该怎么做?这是我到目前为止的代码:HTML和PHP生成分页链接
$page = $_GET["page"];
$pagesQuery = mysql_query("SELECT count(id) FROM(`posts`)");
$pageNum = ceil(mysql_result($pagesQuery, 0)/5);
$start = (($page-1)*5);
$currentname = mysql_query("SELECT * FROM posts LIMIT $start, 5");
while ($row = mysql_fetch_array($currentname)) {
//recieve relevant data.
$title = $row[0];
$desc = $row[13];
$ID = $row[6];
$views = $row[3];
$user = $row[7];
//fetch the last id from accounts table.
$fetchlast1 = mysql_query("SELECT * FROM allaccounts WHERE id=(SELECT MAX(id) FROM allaccounts)");
$lastrow1 = mysql_fetch_row($fetchlast1);
$lastid1 = $lastrow1[6];
//acquire the username of postee.
for ($i1=1; $i1 <= $lastid1; $i1++) {
$currentname1 = mysql_query("SELECT * FROM allaccounts WHERE id=$user");
while ($row1 = mysql_fetch_array($currentname1)) {
$username1 = $row1[0];
}
}
//Format Title, description and view count.
$title2 = rtrim($title);
$donetitle = str_replace(" ", "-", $title2);
$url = "articles/".$ID."/".$donetitle."";
$donetitle = strlen($title) > 40 ? substr($title,0,40)."..." : $title;
$donedesc = '';
if(strlen($desc) > 150) {
$donedesc = explode("\n", wordwrap($desc, 150));
$donedesc1 = $donedesc[0] . '...';
}else{
$donedesc1 = $desc;
}
$finviews = number_format($views, 0, '.', ',');
//Give relevant results
if(stripos($title, $terms) !== false || stripos($desc, $terms) !== false || stripos($username1, $terms) !== false){
if($row[10] == null){
$SRC = "img/tempsmall.jpg";
}else{
$SRC ="generateThumbnailSmall.php?id=$ID";
}
echo "<div id = \"feature\">
<img src=\"$SRC\" alt = \"article thumbnail\" />
</div>
<div id = \"feature2\">
<a href= \"$url\" id = \"titletext\" alt = \"article title\">$donetitle</a>
<p id=\"resultuser\" >$username1</p>
<p id=\"resultp\">$donedesc1</p>
<a href = \"sendflag.php?title=$title&url=$url&id=$ID&userid=$user\" id = \"flag\" alt = \"flag\"><img src=\"img/icons/flag.png\"/></a><b id=\"resultview\">$finviews views</b>
</div>
<div id = \"border\"></div>";
}
}
for ($j=1; $j < $pageNum; $j++) {
echo "<a id =\"\" href=\"searchresults.php?search=".$terms."&page=".$j."\">".$j."</a>";
}
使用此通用脚本实现分页,[https://github.com/rajdeeppaul/Pagination](https:/ /github.com/rajdeeppaul/Pagination) –
不要使用'mysql_ *'函数,它们已被弃用PHP 5.5,并在PHP 7.0中完全删除。改用'MySQLi'或'PDO'。使用上述脚本,您可以使用'MySQLi'或'PDO'驱动程序轻松实现分页。 –