gpt4 book ai didi

php - 如何在wordpress中实现分页

转载 作者:行者123 更新时间:2023-11-30 00:48:22 27 4
gpt4 key购买 nike

我使用page.php在wordpress中开发了一个表单。现在我正在显示从数据库获取的所有表单数据。但是当我显示所有记录时,我的页面会滚动很多。现在我想在我的页面上实现分页。任何人都可以建议我如何在 Word Press 中实现分页。由于我是第一次实现分页,所以我使用本教程从某个地方查找......它工作正常,但没有为下一页记录创建正确的链接。谢谢...

if ( !( isset( $pagenum ) ) ) {
$pagenum = 1;
}

Here we count the number of results Edit $data to be your query

$data = mysql_query("SELECT * FROM red_donation_info") or die( mysql_error() );
$rows = mysql_num_rows( $data );

This is the number of results displayed per page

 $page_rows = 10;

This tells us the page number of our last page

$last = ceil( $rows / $page_rows );

this makes sure the page number isn't below one, or more than our maximum pages

if ( $pagenum < 1 ) {
$pagenum = 1;
} elseif ( $pagenum > $last ) {
$pagenum = $last;
}

This sets the range to display in our query

$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

This is your query again, the same one... the only difference is we add $max into it

$data_p = mysql_query("SELECT * FROM red_donation_info $max") or die(mysql_error()); 

This is where you display your query results

while ( $info = mysql_fetch_array( $data_p ) ) {
print $info['Name'];
echo "<br>";
}
echo "<p>";

This shows the user what page they are on, and the total number of pages

 echo " --Page $pagenum of $last-- <p>";

First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.

if ( $pagenum == 1 ) {
} else {
echo "<a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}

just a spacer

echo " ---- ";

This does the same as above, only checking if we are on the last page, and then generating the Next and Last links

if ( $pagenum == $last ) {
} else {
$next = $pagenum + 1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}

最佳答案

尽量避免使用 mysql_* 函数

$_GET['start'] = isset($_GET['start']) && ctype_digit($_GET['start']) ? abs(@intval($_GET['start'])) : 0;
$select = mysql_query("SELECT COUNT(`id`) FROM `red_donation_info`");
$total = mysql_result($select, 0, 0);
$pages = ceil($total / 10);
$ret = '';
for($i = 1; $i <= $pages; $i++) {
$start = ($i - 1) * 10;
$ret .= "<a href='".$_SERVER['PHP_SELF']."?start=".$start."'>Page ".$i."</a>, ";
}
echo "Page: ".substr($ret, 0, -2);
$mainSelect = mysql_query("SELECT `whatever` FROM `red_donation_info` LIMIT ".$_GET['start'].", 10");
// Loop through data here

这就是我要做的事情..
未经测试,因此可能需要编辑

关于php - 如何在wordpress中实现分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21180045/

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