drjv5597
drjv5597
2014-04-10 11:19

链接到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 duanjian3920 7年前

    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.

    点赞 评论 复制链接分享

相关推荐