gpt4 book ai didi

PHP 分页代码不起作用

转载 作者:行者123 更新时间:2023-11-29 00:37:34 26 4
gpt4 key购买 nike

我无法进行分页。代码显示从 mysql 检索的所有数据。请帮我把它弄好......

函数.php

function pagination($limit=5) //here limit is not taken by the code
{

//$limit=5; if I uncomment this line limit will work
global $admin;

$sql = "SELECT FOUND_ROWS();";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);
$numrows = $row[0];
$pagelinks = "<div class=pagelinks>";
if ($numrows > $limit) {
if(isset($_GET['page'])){
$page = $_GET['page'];
} else {
$page = 1;
}
$currpage = $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING'];
$currpage = str_replace("&page=".$page,"",$currpage);

if($page == 1){
$pagelinks .= "<span class='pageprevdead'>&lt; PREV</span>";
}else{
$pageprev = $page - 1;
$pagelinks .= "<a class='pageprevlink' href='" . $currpage .
"&page=" . $pageprev . "'>&lt; PREV</a>";
}

$numofpages = ceil($numrows / $limit);
$range = $admin['pageRange']['value'];
if ($range == "" or $range == 0) $range = 7;
$lrange = max(1,$page-(($range-1)/2));
$rrange = min($numofpages,$page+(($range-1)/2));
if (($rrange - $lrange) < ($range - 1)) {
if ($lrange == 1) {
$rrange = min($lrange + ($range-1), $numofpages);
} else {
$lrange = max($rrange - ($range-1), 0);
}
}

if ($lrange > 1) {
$pagelinks .= "..";
} else {
$pagelinks .= "&nbsp;&nbsp;";
}
for($i = 1; $i <= $numofpages; $i++){
if($i == $page){
$pagelinks .= "<span class='pagenumdead'>$i</span>";
}else{
if ($lrange <= $i and $i <= $rrange) {
$pagelinks .= "<a class='pagenumlink' href='" . $currpage .
"&page=" . $i . "'>" . $i . "</a>";
}
}
}
if ($rrange < $numofpages) {
$pagelinks .= "..";
} else {
$pagelinks .= "&nbsp;&nbsp;";
}

if(($numrows - ($limit * $page)) > 0){
$pagenext = $page + 1;
$pagelinks .= "<a class='pagenextlink' href='" . $currpage .
"&page=" . $pagenext . "'>NEXT &gt;</a>";
} else {
$pagelinks .= "<span class='pagenextdead'>NEXT &gt;</span>";
}
} else {
$pagelinks .= "<span class='pageprevdead'>
&lt; PREV</span>&nbsp;&nbsp;";
$pagelinks .= "<span class='pagenextdead'>
NEXT &gt;</span>&nbsp;&nbsp;";
}
$pagelinks .= "</div>";
return $pagelinks;
}'

index.php

$sql = "SELECT id FROM articles WHERE is_published=1 " .
"ORDER BY date_published DESC"; //this is the query to retrive data from mysql

$result = mysql_query($sql,$conn);

if (mysql_num_rows($result) == 0) {
echo " <br />\n";
echo " There are currently no articles to view.\n";
} else {
while ($row = mysql_fetch_array($result)) {
outputStory($row['article_id'],TRUE);
}
}

echo pagination($limit);`

最佳答案

此代码不完整。然而,您需要做的第一件事是将 $limit 添加到实际的 sql 查询中:

$sql = "SELECT id FROM articles WHERE is_published=1 " .
"ORDER BY date_published DESC LIMIT {$limit}";

关于PHP 分页代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13640584/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com