Jump to content

Paging System Help


ricbax

Recommended Posts

Does anyone know how I can modify the code below to go from

[color=blue][u]<<[/u] [u]1[/u] [u]2[/u] [u]3[/u] [u]4[/u] [b]5[/b] [u]6[/u] [u]7[/u] [u]8[/u] [u]9[/u] [u]10[/u] [u]11[/u] [u]12[/u] [u]13[/u] [u]14[/u] [u]15[/u] [u]>>[/u][/color]

to something more reduced

[color=blue][u]<<[/u] [u]3[/u] [u]4[/u] [b]5[/b] [u]6[/u] [u]7[/u] [u]8[/u] [u]9[/u] [u]10[/u] [u]11[/u] [u]12[/u] [u]>>[/u][/color]

[code]
<?php
class Pager
  {
      function getPagerData($numHits, $limit, $page)
      {
          $numHits  = (int) $numHits;
          $limit    = max((int) $limit, 1);
          $page    = (int) $page;
          $numPages = ceil($numHits / $limit);

          $page = max($page, 1);
          $page = min($page, $numPages);

          $offset = ($page - 1) * $limit;

          $ret = new stdClass;

          $ret->offset  = $offset;
          $ret->limit    = $limit;
          $ret->numPages = $numPages;
          $ret->page    = $page;

          return $ret;
      }
  }
?>
[/code]

[code]
<?php
            // get the pager input values
    $page = $_GET['page'];
    $limit = 15;
    $pgresult = mysql_query("select count(*) from my_db");
    $total = mysql_result($pgresult, 0, 0);

    // work out the pager values
    $pager  = Pager::getPagerData($total, $limit, $page);
    $offset = $pager->offset;
    $limit  = $pager->limit;
    $page  = $pager->page;
   
    echo "<span style='font-size: 10px;'>Result Pages:&nbsp;&nbsp;";
    // output paging system (could also do it before we output the page content)
    if ($page == 1) // this is the first page - there is no previous page
        echo "";
    else            // not the first page, link to the previous page
        echo "<a href=\"results.php&page=" . ($page - 1) . "\">"." &laquo; "."</a>";

    for ($i = 1; $i <= $pager->numPages; $i++) {
        echo "";
        if ($i == $pager->page)
            echo "<span>$i</span>";
        else
            echo "<a href=\"results.php?&page=$i\">$i</a>";
echo " | ";
    }

    if ($page == $pager->numPages) // this is the last page - there is no next page
        echo "" ;
    else            // not the last page, link to the next page
echo "<a href=\"results.php?&page=" . ($page + 1) . "\">"." &raquo; "."</a></span>";
?>
[/code]
Link to comment
Share on other sites

:D

After plugging away at this for hours I finally came to a [b]rough [/b] solution.

I feel that it really isn't a part of the Pager class, and would appreciate if someone could tell me how I could clean this up and make it a bit more cohesive with the class. Some discussion on this would be great before marking it * SOLVED *. However if I figure something out before the experts on here I will be sure to post my findings as I did here already.

My solution:

[code=PHP SOLUTION]
<?php
// get the pager input values
$page = $_GET['page'];
$limit = 15;
if(empty($start))$start=0;
$result = mysql_query("select count(*) from my_db where mix_id='".$_GET['id']."'");
$total = mysql_result($result, 0, 0);
$per_page = 15; // Number of items to show per page (MUST MATCH $limit, so if $limit = 1, $per_page = 1)
    $showeachside = 20; //  Number of items to show either side of selected page
$cur = ceil($start / $per_page)+1; // Current page number
        $thispage = $PHP_SELF;

// work out the pager values
$pager  = Pager::getPagerData($total, $limit, $page);
$offset = $pager->offset;
$limit  = $pager->limit;
$page  = $pager->page;
   
?>
<?php
$pg=1;
if(($start-$per_page) >= 0)
{
   
    $next = $start-$per_page;
?>
<a href="<?php print("$thispage".($next>0?("?id=".$_GET['id']."&div=open&page=".($cur - 1)."&start=").$next:"?id=".$_GET['id']."&div=open&page=".($cur - 1)."&start="));?>">&laquo;</a>&nbsp;
<?php
}
?><?php
$eitherside = ($showeachside * $per_page);

for($y=0;$y<$total;$y+=$per_page)
{
    $class=($y==$start)?"pageselected":"";
    if(($y > ($start - $eitherside)) && ($y < ($start + $eitherside)))
    {
?>
&nbsp;<a class="<?php print($class);?>" href="<?php print("$thispage".($y>0?("?id=".$_GET['id']."&div=open&page=".($pg)."&start=").$y:"?id=".$_GET['id']."&div=open&page=".($pg)."&start="));?>"><?php print($pg);?></a>&nbsp;
<?php
    }
    $pg++;
}
?><?php
if($start+$per_page<$total)
{
?>
&nbsp;<a href="<?php print("$thispage?id=".$_GET['id']."&div=open&page=".($cur + 1)."&start=".max(0,$start+$per_page));?>">&raquo;</a></span>
<?php
}
?>
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.