SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Jan 2007
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Multiple Page Help

    I started off earlier this week trying to work out how to create multiple pages with PHP. I have got something working but I would like to expand on this further, but I am afraid with my limited knowledge I am stuck.

    Here is my code that works fine at the moment, and it gives a first, next, previous and last link and also gives a link directly to every page.


    Code:
    <?php
    $pagenum = $_GET['pagenum'];
    $dbcnx = @mysql_connect("");
    
    if (!$dbcnx) {
    echo( "<P>Unable to connect to the database server at this time.</P>" );
    exit();
    }
    
    if (!@mysql_select_db("tpvhc", $dbcnx) ) {
      echo( "<P>Unable to locate the Correct database at this time.</P>" );
      exit();
    }
    
    
    
    
    // how many rows to show per page
    $rowsPerPage = 5;
    
    // by default we show first page
    $pageNum = 1;
    
    // if $_GET['page'] defined, use it as page number
    if(isset($_GET['page']))
    {
        $pageNum = $_GET['page'];
    }
    
    // counting the offset
    $offset = ($pageNum - 1) * $rowsPerPage;
    
    $query = " SELECT Cottage_ID, Cottage_Name, Bedrooms FROM Cottage " . " LIMIT $offset, $rowsPerPage";
    $result = mysql_query($query) or die('Error, query failed');
    
    echo '<table class="table">';
    echo '<tr><th>Cottage ID</th><th>Cottage Name</th><th>Bedrooms</th></tr>';
    while($row = mysql_fetch_array($result))
    {
    	echo '<tr>';
    	echo '<td>' . $row['Cottage_ID'] . '</td>';
    	echo '<td>' . $row['Cottage_Name'] . '</td>';
    	echo '<td>' . $row['Bedrooms'] . '</td>';
    	echo '<tr>';
    }
    
    echo '<table>';
    
    
    
    
    
    $maxPage = ceil($numrows/$rowsPerPage);
    
    
    // how many rows we have in database
    $query   = "SELECT COUNT(Cottage_ID) AS numrows FROM Cottage";
    $result  = mysql_query($query) or die('Error, query failed');
    $row     = mysql_fetch_array($result, MYSQL_ASSOC);
    $numrows = $row['numrows'];
    
    // how many pages we have when using paging?
    $maxPage = ceil($numrows/$rowsPerPage);
    
    // print the link to access each page
    $self = $_SERVER['PHP_SELF'];
    $nav  = '';
    
    
    
    //code to put the page links in
    for($page = 1; $page <= $maxPage; $page++)
    {
       if ($page == $pageNum)
       {
          $nav .= " $page "; // no need to create a link to current page
       }
       else
       {
          $nav .= " <a href=\"$self?page=$page\">$page</a> ";
       }
    }
    //end of page links code
    
    
    
    
    
    // creating previous and next link
    // plus the link to go straight to
    // the first and last page
    
    if ($pageNum > 1)
    {
       $page  = $pageNum - 1;
       $prev  = " <a href=\"$self?page=$page\">[Prev]</a> ";
    
       $first = " <a href=\"$self?page=1\">[First Page]</a> ";
    }
    else
    {
       $prev  = '&nbsp;'; // we're on page one, don't print previous link
       $first = '&nbsp;'; // nor the first page link
    }
    
    if ($pageNum < $maxPage)
    {
       $page = $pageNum + 1;
       $next = " <a href=\"$self?page=$page\">[Next]</a> ";
    
       $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
    }
    else
    {
       $next = '&nbsp;'; // we're on the last page, don't print next link
       $last = '&nbsp;'; // nor the last page link
    }
    
    // print the navigation link
    echo $first . $prev . $nav . $next . $last;
    
    
    ?>
    
    
    
    </p>
    </body>
    </html>



    This works fine at the moment as I only have 26 results but if I have 500 results then the direct page links will be massive and look too much if I have 5 results a page there will be 100 of these links.

    So what I would like to do is limit the number of these links that appear ideally 3 at a time so if you are on page 1 it shows a link to page 2,3,4 and if you are on page 6 it would show 3,4,5 7,8,9. If page 10 was our last page and you were on page 9 it would show 6,7,8 10.

    Now I am sure this is quite hard to do but possibly so I wondered if someone could point me in the right direction or give me a starting point and something to work with.

    Cheers for any help

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like this should work:
    Code php:
    ...................................
    # the number page links to show in a time
    $showOffset = 10;
    if($pageNum + $showOffset > $maxPage)
    	$offset_val = $maxPage;
    else
    	$offset_val = ($pageNum + $showOffset);
    for($page = $pageNum; $page <= $offset_val; $page++){
       if ($page == $pageNum){
          $nav .= " $page "; // no need to create a link to current page
       }
       else{
          $nav .= " <a href=\"$self?page=$page\">$page</a> ";
       }
    }
    .................
    I hope you can find where i have made changes in your code.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Addict
    Join Date
    Jan 2007
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats brilliant just going through and trying to understand it at the moment.

    Is it possible if you are on page 6 to show 345 and then 789???

    Code:
    $showOffset = 3;
    if($pageNum + $showOffset > $maxPage)
        $offset_val = $maxPage;
    else
        $offset_val = ($pageNum + $showOffset);
    for($page = $pageNum; $page <= $offset_val; $page++ || $page >= $offset_val; $page--)
    
    {
       if ($page == $pageNum){
          $nav .= " $page "; // no need to create a link to current page
       }
       else{
          $nav .= " <a href=\"$self?page=$page\">$page</a> ";
       }
    }
    I tried with the above code but to no avail. I think maybe i need another if statement in there but I have no idea where to put it or if that is the correct way.

    Cheers


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •