gpt4 book ai didi

php - 我想使用 MySQL LIMIT 在分页中每页显示 2 条记录,

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

我必须显示,在php中分页显示2到5。我在这里列出的代码对我有用。老实说我已经改变了源代码。知道我想知道。如何在此代码中引入 echo“在总共 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
}
?>

最佳答案

虽然评论已被删除,但您希望快速解决您的问题。不幸的是,我认为使用像 mysql_* 这样已弃用的代码是一个坏主意。因此,我为您编写了一个新系统,它将使用 PDO 代替。请仔细阅读代码并在需要的地方进行编辑。例如,我不知道您的列名称,因此此代码无法即时运行!

<!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>

关于php - 我想使用 MySQL LIMIT 在分页中每页显示 2 条记录,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30343180/

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