简体   繁体   中英

How to limit pages in pagination system PHP/MySQL?

I have a pagination system for my users' dashboard, and basically what I'm worried about is when the users' friends start posting to the point where there's like 50-60-70 pages.

I want to have it like so: 1 2 3 4 5 ... 91 and then when users load a different page, have it like so: 1 ... 22 23 24 ... 91

or something in that kind of matter. That way it doesn't try to list all of the pages at once and get really ugly.

I'm not very familliar with pagination, and I had an old friend help me with this system, so sorry if this is a noobish question. I looked around online and stuff and couldn't seem to find a way to implement what I'm trying to do in with my system.

Should I remake the whole thing? If so, what would be the best way of going about doing this? Here's the current function that I'm working with:

function friendstatus(){
    global $session_user_id;
    $per_page = 15;
    $from_where = "FROM posts WHERE userid in (SELECT friend_id FROM friendship WHERE user_id = $session_user_id AND pending = 0 OR friend_id = $session_user_id) AND visible != 0 ORDER BY `timestamp` DESC";
    $sql = "SELECT COUNT(*) ".$from_where;
    $res = mysql_query($sql) or trigger_error("Error.");
    $row = mysql_fetch_row($res);
    $total_rows = $row[0];
    if($total_rows != 0){
        if(isset($_GET['page'])) $CUR_PAGE = intval($_GET['page']); else $CUR_PAGE=1;
        $start = abs(($CUR_PAGE-1)*$per_page);
        $sql = "SELECT * $from_where LIMIT $start,$per_page";
        $res = mysql_query($sql) or trigger_error("cant get actual data");
        while($row=mysql_fetch_array($res)) $DATA[++$start]=$row;
        $uri = strtok("/dash","?")."/";
        $tmpget = $_GET;
        unset($tmpget['page']);
        if($tmpget){ $uri .= http_build_query($tmpget)."/"; }
        $num_pages=ceil($total_rows/$per_page);
        for($i=1;$i<=$num_pages;$i++) $PAGES[$i]=$uri.$i; ?>

        Pages:<? foreach ($PAGES as $i => $link): ?> <? if ($i == $CUR_PAGE): ?><b><?=$i?></b> <? else: ?><a href="<?=$link?>"><?=$i?></a> <? endif ?><? endforeach ?> | Total Posts: <b><?=$total_rows?></b>
        <hr color="#1F1F1F" width="100%"><br><?foreach($DATA as $i => $row):?>

        //show the statuses

        <? endforeach ?>

        Pages:<? foreach ($PAGES as $i => $link): ?> <? if ($i == $CUR_PAGE): ?><b><?=$i?></b> <? else: ?><a href="<?=$link?>"><?=$i?></a> <? endif ?><? endforeach ?> | Total Posts: <b><?=$total_rows?></b>
    <? } //end if $total_rows != 0 
} //end function 

Begin by defining some range around the current page you wish to display.

$RANGE = 2; //show 2 pages before and after current

Then determine the start and end pages for this range:

$start = max($CUR_PAGE-$RANGE,1);
$end = min($CUR_PAGE+$RANGE,count($PAGES));

Then replace your

foreach($PAGES as $i=>$link)

with

for($i=$start;$i<=$end;$i++) //references to $link in the body become $PAGES[$i] 

For the static "first x pages" and "last x pages" you'll have to do some checks to ensure you don't end up with repeated pages like 1...12345...91. A simple if statement will suffice here if you're only interested in having links to the first and last page, you'll have to do some minor index adjusting in the event you want to show the first and last 3 pages for example.

I was able to achieve basically what I wanted by throwing in a few if statements.

Replacing the part that lists the pages with...

<a href="https://mysite/dash/1">First</a> | <? foreach ($PAGES as $i => $link): ?><? if ($i == $CUR_PAGE): ?><b><?=$i?></b> <? endif ?><? if($i == $CUR_PAGE+1):?><a href="<?=$link?>"><?=$i?></a><? endif ?><? endforeach ?> | <a href="https://mysite/dash/<?=$num_pages?>">Last</a> | Total Posts: <b><?=$total_rows?></b>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM