- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我必须显示,在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/
好吧,我对我的页面发生的事情有点困惑。我在底部有页码, table 上摆满了 23 种不同的元素。 每个页面应一次显示 5 个项目。我显示的页面发送了一个带有 pagenumber = 不管怎样的 g
几周前我开始学习 Ada。我知道 limited 在某些情况下声明了一个有限类型,不允许复制对象 来自 Ada Reference Manual 2012 7.5 1/2 A limited type
我想按 DESC 选择日期并限制为最后 3 个,然后我需要基本上翻转最后 3 个结果,以便它们按 ASC 排序 任何帮助将不胜感激,我正在使用 postgresql 最佳答案 SELECT * FRO
SELECT * FROM mm_tfs WHERE product_slug LIKE '%football%' AND schoolid = '8' AND category_id ='2
SELECT * FROM mm_tfs WHERE product_slug LIKE '%football%' AND schoolid = '8' AND category_id ='2
我正在尝试访问按“日期”键排序的表中恒定数量的最新文档。请注意,不幸的是,日期是被实现的(不是由我......),使得该值设置为字符串,例如“2014-01-14”,或者有时“2014-01-14 2
我目前正在使用具有限制 cpu、限制内存以及保留 cpu 和内存的 Docker Swarm。 完成测试后,我想删除这些配置。我找不到任何有关如何删除这些的文章。 是否有办法通过更新来删除这些设置而不
我目前正在使用具有限制 cpu、限制内存以及保留 cpu 和内存的 Docker Swarm。 完成测试后,我想删除这些配置。我找不到任何有关如何删除这些的文章。 是否有办法通过更新来删除这些设置而不
我必须对我的数据应用分页。我通过复杂的连接查询获取数据,没有任何子查询,只有简单的连接。 假设这个查询[这是一个非常简单的查询,我有一个比这个复杂的查询] SELECT states.state
我经常制作条形图,并将条形图的值额外包含为注释 (geom_text)。通常,我更喜欢这些值右对齐(与将标签放在条形顶部相反)。在绘制多面条形图时,我将这些值放在每个组中的最大值(我之前计算过)加上我
delivery-limit 和 x-delivery-limit 有什么区别? 当我将 x-delivery-limit 设置为 RabbitMQ 队列参数时,我可以看到它限制了我的消息重新排队尝试
delivery-limit 和 x-delivery-limit 有什么区别? 当我将 x-delivery-limit 设置为 RabbitMQ 队列参数时,我可以看到它限制了我的消息重新排队尝试
我正在使用 PostgreSQL 9.3。这应该在具有 100,000 多行的任何表上重现。 EXPLAIN ANALYZE 显示使用 LIMIT 2 扫描了更多行,但我不明白为什么。 限制 1: E
我正在尝试找出是否可以在 PHP 中全局设置和取消设置 MySQL 结果的默认限制。 一些可能看起来像的伪代码: $pdo->prepare('SELECT * FROM example'); $pd
我有下面的代码片段表 在这里我必须对投票前 3 个值求和。 假设 product_id 3030 vote 列的总和为 8.1 和 3671 总和 是 5.2 在这里,我必须获得前 3 个 produ
我正在使用全文搜索来提取行。 我根据分数 (ORDER BY SCORE) 对行进行排序,然后在前 20 行 (LIMIT 20) 中,我想对结果集进行兰德 (RAND)。 因此,对于任何特定的搜索词
帮助创建搜索条件 SELECT * FROM mlt_adr_city WHERE name LIKE "Text%" AND region_id = 59 AND id <> 0 IF (name
MySQL 查询示例: SELECT message_id, message_text FROM messages LIMIT 0 , 30 我得到的这个提示是错误的: HIN
我注意到如果我将查询限制为 1 个而不是 5 个,速度会急剧下降。 SELECT he. * FROM homematic_events he WHERE he.homematic_devices_i
我需要从我的表中获取最后一个 ID,以便我可以在另一个函数中使用它我在我的存储库中创建了这个函数,但我没有工作,它显示了一个错误: [Syntax Error] line 0, col 60: Err
我是一名优秀的程序员,十分优秀!