1

我无法进行分页。该代码显示从 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;
}'

索引.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);`
4

2 回答 2

1

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

$sql = "SELECT id FROM articles WHERE is_published=1 " .
   "ORDER BY date_published DESC LIMIT {$limit}";
于 2012-11-30T07:34:54.410 回答
0

尝试这个。只需创建一个名为 player 的 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>
于 2012-11-30T08:10:44.353 回答