我必须显示,在php中显示2到5的分页。我在这里列出的代码对我有用。诚实说我已经改变了源代码。知道我想知道。如何引入回声"在5"的总记录中显示1到5;在这段代码中。我不知道如何实现这一点。
我的代码在这里:
<?php
$rowstodisplay =2;
$currentPage = $_SERVER["PHP_SELF"];
$pid = 0;
if (isset($_GET['pid']))
{
$pid = $_GET['pid'];
}
$startrow = $pid * $rowstodisplay;
$selectorders=sprintf("SELECT * FROM parent_cat ORDER BY p_cat ASC");
$results = mysql_query($selectorders) or die(mysql_error());
$tot_rsselect = mysql_num_rows($results);
$query_limit_results = sprintf("%s LIMIT %d, %d", $selectorders, $startrow, $rowstodisplay);
$myorderquery = mysql_query($query_limit_results) or die(mysql_error());
$totalpages = ceil($tot_rsselect/$rowstodisplay)-1;
$navblock = "";
if ($pid <= $totalpages)
{
if ($pid > 0)
{
$navblock .= '<a href="'.$currentPage.'?pid=0">First</a> | <a href="'.$currentPage.'?pid='.max(0,$pid-1).'">Previous</a>';
}
if ( ($pid > 0)&&($pid < $totalpages) )
{
$navblock .= " | ";
}
if ($pid < $totalpages)
{
$navblock .= '<a href="'.$currentPage.'?pid='.min($totalpages, $pid+1).'">Next</a> | <a href="'.$currentPage.'?pid='.$totalpages.'">Last</a>';
}
}
$nolinks = 15;
$navpageno = "";
if ($pid <= $totalpages)
{
for($i=max(0,$pid-$nolinks); $i<=min($totalpages, $pid+$nolinks); $i++)
{
$pageno = $i+1;
if($pid == $i)
{
$navpageno .= '<li class="navigation2">'.$pageno.' </li>';
}
else
{
$navpageno .= '<li class="navigation"><a href="'.$currentPage.'?pid='.$i.'">'.$pageno.'</a></li>';
}
}
}
if($navblock != "")
{
?>
<ul class="navigationholder"><?php echo $navpageno; ?></ul>
<?php
}
?>
答案 0 :(得分:1)
虽然评论被发现,但您希望快速修复您的问题。不幸的是,我觉得使用像mysql_*
这样的弃用代码是一个坏主意。因此,我为您编写了一个新系统,它将使用PDO
代替。{1}}。请仔细阅读代码并编辑nessesary。例如,我不知道您的列名称,因此此代码无法正常运行!
<!DOCTYPE html>
<html>
<head>
<title>Pagination</title>
</head>
<body>
<?php
// Database Settings
$dbhost = 'localhost';
$dbuser = '';
$dbpass = '';
$dbname = '';
// Establish Connection to the Database
$dbh = new PDO('mysql:host='. $dbhost .';dbname='. $dbname, $dbuser, $dbpass, array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
// Selecting the data from table but with limit
$query = 'SELECT * FROM parent_cat ORDER BY p_cat ASC LIMIT :start, :page';
// Prepare query
$pre = $dbh->prepare($query);
// Binding values
$pre->bindParam(':start', $start_from);
$pre->bindParam(':page', $per_page);
// Results per page
$per_page=2;
if (isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page=1;
}
// Page will start from 0 and Multiple by Per Page
$start_from = ($page-1) * $per_page;
?>
<!-- Start building HTML table -->
<table>
<?php
// Execute query
try {
$pre->execute();
// Fetch all results
$results = $pre->fetchAll(PDO::FETCH_ASSOC);
// Loop through results
foreach($results as $data){
// Display results in HTML table
echo "<tr>";
// Add/Remove your column names here
echo "<td>". $data['column_name_here'] ."</td>";
echo "<td>". $data['column_name_here'] ."</td>";
echo "<td>". $data['column_name_here'] ."</td>";
// Close HTML table row
echo "</tr>";
}
} catch (PDOException $e) {
echo 'MySQL query error: ' . $e->getMessage();
}
?>
<!-- End building HTML table -->
</table>
<div>
<?php
// Now select all data from table
$query = 'SELECT * FROM parent_cat';
// Prepare the query
$pre = $dbh->prepare($query);
// Execute the query
try {
$pre->execute();
// Count the results
$total_records = $pre->rowCount();
// Keep a record of total number of rows
$total_rows = $total_records;
// Using ceil function to divide the total records on per page
$total_pages = ceil($total_records / $per_page);
// Going to first page
echo "<center><a href='pagination.php?page=1'>First Page</a> ";
// Showing number of pages in between last page
for ($i=1; $i<=$total_pages; $i++){
echo "<a href='pagination.php?page=". $i ."'>". $i ."</a> ";
}
// Going to last page
echo "<a href='pagination.php?page=". $total_pages ."'>Last Page</a></center> ";
} catch (PDOException $e) {
echo 'MySQL query error: ' . $e->getMessage();
}
// Calculate first and last item on current page
$first = $page * $per_page - $per_page;
$last = $page * $per_page;
// Make sure the number can never be more than the total numer of rows
if($last > $total_rows){
$last = $total_rows;
}
// Showing the results
echo "<br />";
echo "<center>Showing ". $first ." to ". $last ." in total record of ". $total_rows ."</center>";
?>
</div>
</body>
</html>