好的,我会在这里粘贴我的代码,就像一个人选择一个类别,然后他登陆另一个页面,现在问题是因为mysql_fetch_rows选择所有类别的所有行,所以如果有10个条目,每个类别5,如果我设置为每页显示1个帖子而不是显示5个页面,它显示10个页面,但在第5页后,我得到一个PHP错误。谢谢你,这是我的代码
$pagination_shoot = "SELECT id, po_title, po_category FROM tbl_posts WHERE po_category = '{$category}'";
$page_dump = mysql_query($pagination_shoot, $db_connect);
$no_rows = mysql_fetch_row($page_dump);
$numrows = $no_rows[0];
$rows_per_page = 1;
$lastpage = ceil($numrows/$rows_per_page);
$page = (int)$page;
if ($page > $lastpage) {
$page = $lastpage;
} // if
if ($page < 1) {
$page = 1;
} // if
$limit = 'LIMIT ' .($page - 1) * $rows_per_page .',' .$rows_per_page;
//Run Post Query
$data_posts = "SELECT id, po_title, po_category FROM tbl_posts WHERE po_category = '{$category}' {$limit}"; //Post Query
$fetch_data_posts = mysql_query($data_posts, $db_connect);
while ($list_posts = mysql_fetch_array($fetch_data_posts))
答案 0 :(得分:0)
在行之间阅读,我认为你要做的是从该数据库中获取特定页面的结果,同时知道总共有多少结果,这样你就可以计算出会有多少页面。以下是使用SQL_CALC_FOUND_ROWS
:
// Populate these variables however you see fit (eg through $_GET)
$rows_per_page = 1;
$current_page = 1;
// -----------
// Make sure the page number is >= 1
$current_page = (int) $current_page;
if ($current_page < 1) $current_page = 1;
// Calculate values for LIMIT clause
$limitQty = (int) $rows_per_page;
$limitBase = ($current_page - 1) * $limitQty;
// Run the query
$query = "
SELECT SQL_CALC_FOUND_ROWS
`id`, `po_title`, `po_category`
FROM `tbl_posts`
WHERE `po_category` = '".mysql_real_escape_string($category, $db_connect)."'
LIMIT $limitBase, $limitQty
";
$result = mysql_query($query, $db_connect);
// Store the results in an array and free the MySQL resource
$data_posts = array();
while ($row = mysql_fetch_array($result)) {
$data_posts[] = $row;
}
mysql_free_result($result);
// Get the total number of rows
$query = "
SELECT FOUND_ROWS()
";
$result = mysql_query($query, $db_connect);
$numrows = mysql_fetch_row($result);
$total_rows = $numrows[0];
mysql_free_result($result);
// Calculate the total number of pages
$total_pages = ceil($total_rows / $rows_per_page);
if ($current_page > $total_pages) {
// Handle a request that asked for a page which is greater than the total number of pages
// You could display an error message, redirect to a page within range, or run another query
// to fetch the rows for the last page.
}
// Now you can loop the data results and do whatever you want with them
foreach ($data_posts as $list_posts) {
// ...
}