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.

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

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能