Jump to content

Recommended Posts

Anybody know how to modify the code below so that I can limit the number of pages displayed per page?  The current system display ALL the pages on ONE page.  I would like to set a limit to 10 pages like this:

 

Page 1 of 2000

FIRST PREV  page1, page2, page3, page4, page5, page6, page7, page8, page9, page10 NEXT LAST

 

:-\  ???

Nathan

 

I would like to modify my current pagation system because it works well

###pager.php####

<?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;

      }

  }

?>

 

 

###page.php###

<?php

require('pager.php'); // load pager

// get the pager input values

    $page = $_GET['page'];

    $limit = 10;

    $result = mysql_query("SELECT count(*) FROM table");

    $total = mysql_result($result, 0, 0);

// work out the pager values

    $pager  = Pager::getPagerData($total, $limit, $page);

    $offset = $pager->offset;

    $limit  = $pager->limit;

    $page  = $pager->page;

// use pager values to fetch data

 

$sql = "SELECT * FROM table ORDER BY sort_date DESC LIMIT $offset, $limit";

$res = mysql_query($sql);

if(mysql_num_rows($res))

{

while($row = mysql_fetch_array($res))

{

?>

<!--TABLE LOOP START-->

<tr>

<td><?php echo $row[0];?></td>

<td><?php echo $row[1];?></td>

<td><?php echo $row[2];?></td>

<td><?php echo $row[3];?></td>

<td><?php echo $row[4];?></td>

<td><?php echo $row[5];?></td>

<td><?php echo $row[6];?></td>

<td><?php echo $row[7];?></td>

<td><?php echo $row[8];?></td>

</tr>

<!--TABLE LOOP END-->

<?php

}

}

else

{

echo "No sites entered into the database...";

}

?>

</table>

<?php

  // use $result here to output page content

// output paging system (could also do it before we output the page content)

echo "<div id=\"pagation\">";

 

    if ($page == 1) // this is the first page - there is no previous page

        echo "Previous";

    else            // not the first page, link to the previous page

        echo "<a href=\"page2.php?page=" . ($page - 1) . "\">Previous</a>";

 

    for ($i = 1; $i <= $pager->numPages; $i++) {

        echo " | ";

        if ($i == $pager->page)

            echo "Page $i";

        else

            echo "<a href=\"page2.php?page=$i\">Page $i</a>";

    }

 

    if ($page == $pager->numPages) // this is the last page - there is no next page

        echo " | Next";

    else            // not the last page, link to the next page

        echo " | <a href=\"page2.php?page=" . ($page + 1) . "\">Next</a>";

echo "</div>";

?>

</body>

um, i havent read the code.... but if your code lists eg pgs like:

 

< 1 2 3 4 5 6 7 > then all you have to go is limit the range of your numbers.... eg:

 

just go and list current page + 4 pages, current page num( not link), currentpage - 4 pages.... then add the next previous, adn more or less links

 

 

should be fairly easy if thats what your trying to do

 

 

gdlk

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.