php - 如何在wordpress中实现分页

标签 php mysql wordpress

我使用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/

相关文章:

php - 字段 'user_id' 为空

php - 获取全局用户的本地时间并以本地时区格式显示给其他时区用户

php - 如何创建 nouser/nogroup 以便 WordPress 可以写入文件?

javascript - 我怎样才能让这个 jQuery 脚本在 WordPress 上运行?

html - 仅 1 张图像的对齐不正确

php - 在 PHP 中使用重命名函数时出错

php - 多个 mysql_fetch_array()

php - 在 PHP/MySQL 中访问最后创建的行

php-mysqlnd 启动失败

javascript - 使用 Ajax 如何将表单中的下拉菜单选择值与数据库中的记录相匹配,并获取关联的记录/行以预填充表单?