drjv5597 2014-04-10 11:19
浏览 42
已采纳

链接到php分页中的另一个页面?

I have a code with query for in php paging

$page = (isset($_GET['page'])) ? $_GET['page'] : 1;

$startPoint = $page - 1;
$sql="SELECT * FROM ` admin_crmf_poc_event_history` 
where $condition
order by event_date asc
LIMIT $startPoint,30";
$result = mysql_query($sql);

and for creating link to next page, i use

<a href="index.php?page=<?php echo $page - 29?>">Prev</a>
<a href="index.php?page=<?php echo $page + 29?>">Next</a>

but I give link index.php which shows the whole values from the start. dont know how to give link of next page so the rest of values are shown. Please help??

  • 写回答

1条回答 默认 最新

  • duanjian3920 2014-04-10 11:23
    关注

    try it like this

    $page = (isset($_GET['page']) && (int)$_GET['page']>0) ? (int)$_GET['page'] : 1);
    
    $startPoint = ($page*30) - 30;
    $sql="SELECT * FROM ` admin_crmf_poc_event_history` 
    where $condition
    order by event_date asc
    LIMIT $startPoint,30";
    $result = mysql_query($sql);
    
    <?php if($page>1){?><a href="index.php?page=<?php echo $page - 1; ?>">Prev</a><?php } ?>
    <a href="index.php?page=<?php echo $page + 1; ?>">Next</a>
    

    so what I did is first I added (int) before your $_GET['page'] to cast the $_GET value to int, second thing is I multiplied $page by how many rows per page you want then subtracted rows per page so, if you are at page 1 your start point will be 1*30-30=0 at page 2 it will be 2*30-30=30 etc... Then all you have to do with page links is subtract 1 for previous page and add 1 for next page.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程