PHP分页代码不工作
我无法分页。代码显示从mysql检索的所有数据。请帮我把它弄对 function.phpPHP分页代码不工作,php,mysql,pagination,Php,Mysql,Pagination,我无法分页。代码显示从mysql检索的所有数据。请帮我把它弄对 function.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
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'>< PREV</span>";
}else{
$pageprev = $page - 1;
$pagelinks .= "<a class='pageprevlink' href='" . $currpage .
"&page=" . $pageprev . "'>< 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 .= " ";
}
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 .= " ";
}
if(($numrows - ($limit * $page)) > 0){
$pagenext = $page + 1;
$pagelinks .= "<a class='pagenextlink' href='" . $currpage .
"&page=" . $pagenext . "'>NEXT ></a>";
} else {
$pagelinks .= "<span class='pagenextdead'>NEXT ></span>";
}
} else {
$pagelinks .= "<span class='pageprevdead'>
< PREV</span> ";
$pagelinks .= "<span class='pagenextdead'>
NEXT ></span> ";
}
$pagelinks .= "</div>";
return $pagelinks;
}'
$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);`
function pagination($limit=5)//此处代码不接受限制
{
//$limit=5;如果我取消注释,该行限制将起作用
全球$admin;
$sql=“选择已找到的行();”;
$result=mysql\u query($sql)或die(mysql\u error());
$row=mysql\u fetch\u数组($result);
$numrows=$row[0];
$pagelinks=“”;
如果($numrows>$limit){
如果(isset($_GET['page'])){
$page=$_GET['page'];
}否则{
$page=1;
}
$currpage=$\u服务器['PHP\u SELF']。“?”$\u服务器['QUERY\u STRING'];
$currpage=str_replace(“&page=”.$page,”,$currpage);
如果($page==1){
$pagelinks.=“上一页”;
}否则{
$pageprev=$page-1;
$pagelinks.=“”;
}
$numopages=ceil($numrows/$limit);
$range=$admin['pageRange']['value'];
如果($range==“”或$range==0)$range=7;
$lrange=max(1,$page-($range-1)/2));
$rrange=min($numopages,$page+($range-1)/2));
如果(($rrange-$lrange)<($range-1)){
如果($lrange==1){
$rrange=min($lrange+($range-1),$numofpages);
}否则{
$lrange=max($rrange-($range-1),0);
}
}
如果($lrange>1){
$pagelinks.=”;
}否则{
$pagelinks.=“”;
}
对于($i=1;$i此代码不完整。但是,您需要做的第一件事是将$limit添加到实际的sql查询中:
$sql = "SELECT id FROM articles WHERE is_published=1 " .
"ORDER BY date_published DESC LIMIT {$limit}";
此代码不完整。但是,您需要做的第一件事是将$limit添加到实际的sql查询中:
$sql = "SELECT id FROM articles WHERE is_published=1 " .
"ORDER BY date_published DESC LIMIT {$limit}";
试试这个。
只需创建一个名为players的mysql表,其中包含3个字段:-id、firstname、lastname,以检查代码的工作方式
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>View Records</title>
</head>
<body>
<?php
/*
VIEW-PAGINATED.PHP
Displays all data from 'players' table
This is a modified version of view.php that includes pagination
*/
// connect to the database
include('dbconfig.php');
// number of results to show per page
$per_page = 2;
// figure out the total pages in the database
$result = mysql_query("SELECT * FROM players");
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $per_page);
// check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
// make sure the $show_page value is valid
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
// error - show first set of results
$start = 0;
$end = $per_page;
}
}
else
{
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
// display pagination
echo "<p><a href='view.php'>View All</a> | <b>View Page:</b> ";
for ($i = 1; $i <= $total_pages; $i++)
{
echo "<a href='view-paginated.php?page=$i'>$i</a> ";
}
echo "</p>";
// display data in table
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++)
{
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) { break; }
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . mysql_result($result, $i, 'id') . '</td>';
echo '<td>' . mysql_result($result, $i, 'firstname') . '</td>';
echo '<td>' . mysql_result($result, $i, 'lastname') . '</td>';
echo '<td><a href="edit.php?id=' . mysql_result($result, $i, 'id') . '">Edit</a></td>';
echo '<td><a href="delete.php?id=' . mysql_result($result, $i, 'id') . '">Delete</a></td>';
echo "</tr>";
}
// close table>
echo "</table>";
// pagination
?>
<p><a href="new.php">Add a new record</a></p>
</body>
</html>
查看记录
试试这个。
只需创建一个名为players的mysql表,其中包含3个字段:-id、firstname、lastname,以检查代码的工作方式
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>View Records</title>
</head>
<body>
<?php
/*
VIEW-PAGINATED.PHP
Displays all data from 'players' table
This is a modified version of view.php that includes pagination
*/
// connect to the database
include('dbconfig.php');
// number of results to show per page
$per_page = 2;
// figure out the total pages in the database
$result = mysql_query("SELECT * FROM players");
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $per_page);
// check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
// make sure the $show_page value is valid
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
// error - show first set of results
$start = 0;
$end = $per_page;
}
}
else
{
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
// display pagination
echo "<p><a href='view.php'>View All</a> | <b>View Page:</b> ";
for ($i = 1; $i <= $total_pages; $i++)
{
echo "<a href='view-paginated.php?page=$i'>$i</a> ";
}
echo "</p>";
// display data in table
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";
// loop through results of database query, displaying them in the table
for ($i = $start; $i < $end; $i++)
{
// make sure that PHP doesn't try to show results that don't exist
if ($i == $total_results) { break; }
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . mysql_result($result, $i, 'id') . '</td>';
echo '<td>' . mysql_result($result, $i, 'firstname') . '</td>';
echo '<td>' . mysql_result($result, $i, 'lastname') . '</td>';
echo '<td><a href="edit.php?id=' . mysql_result($result, $i, 'id') . '">Edit</a></td>';
echo '<td><a href="delete.php?id=' . mysql_result($result, $i, 'id') . '">Delete</a></td>';
echo "</tr>";
}
// close table>
echo "</table>";
// pagination
?>
<p><a href="new.php">Add a new record</a></p>
</body>
</html>
查看记录
我在sql中有10条记录。它在索引页上显示sql中显示的所有数据。我在分页时单击“下一步”,它会显示带我到第二页的相同数据。我在sql中有10条记录。它在索引页上显示sql中显示的所有数据。我在分页时单击“下一步”,它会显示带我到第二页的相同数据。它给我这个信息消息“警告:mysql\u num\u rows():提供的参数不是有效的mysql结果资源”由于某种原因,您无法获得mysql结果,请回显您的查询,然后退出(mysql\u error())$limit可能没有定义,在这种情况下会产生语法错误;请使用echo$sql
确保语句看起来正确。请向我发送以下消息“警告:mysql\u num\u rows():提供的参数不是有效的mysql结果资源”您由于某种原因无法获得mysql结果,请回送您的查询,然后退出(mysql\u error())$limit可能没有定义,在这种情况下会产生语法错误;请使用echo$sql
确保语句看起来正确