dpo15099 2013-09-28 19:50
浏览 47

将下一个按钮添加到php分页脚本。 它简短而简单

This is the first pagination script I've coded with help from a friend. I customized it a bit and want to also add NEXT and PREVIOUS buttons. Here is my code.

$per_page = 6;

$pages_query = mysql_query("SELECT COUNT(`user_id`) FROM `users`");
$pages = ceil(mysql_result($pages_query, 0) / $per_page);

$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;

$query = mysql_query("SELECT `username` FROM `users` WHERE `active` = 1 LIMIT $start,         $per_page");
while ($query_row = mysql_fetch_assoc($query)) {
  echo '<p>', $query_row['username'] , '</p>';
}

//previous (this is where the previous button would go)

if ($pages >=1 && $page <=$pages) {
  for ($x=1; $x<=$pages; $x++) {
    echo ($x == $page) ? '<strong><a href="?page='.$x.'">'.$x.'</a></strong> ' : '<a         href="?page='.$x.'">'.$x.'</a> ';
  }
}

I've simplified the script and have removed the last page first page buttons. I also exluded the php tags and mysql connection

  • 写回答

1条回答 默认 最新

  • duanchique1196 2013-11-21 20:47
    关注

    This is pretty simple

    add this before loop starts with $_GET global value

    for your previous if(isset($_GET['page']) && $_GET['page'] > 1){ href($_GET['page'] -1) }

    for your next if(isset($_GET['page']) && $_GET['page'] < $pages){ href($_GET['page'] +1) } you can add this with subtract and add operator

    however thanks for sharing the code i got really easy way to use your code with my custom php application too :)

    enter image description here

    评论

报告相同问题?

悬赏问题

  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题