Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jun 2004
    Posts
    128
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Displaying database data over X No. of pages (PHP help)

    I know I am going to confuse all of you now. I'll try to explain the best I can.

    I am thinking about making a message baord, and I think I have the knowledge now in order to do it, except this one tiny thing: I don't know how to code a "Page" script. Meaning, if there are a certain amount of topics on a page, lets say 50, and I want to limit it to 10 topics a page, and have something like "Page: 1 | 2 | 3 | 4 | 5 | 6" at the bottom displaying the number of pages. Now, I already know it isn't the MySQL Query LIMIT, because that cuts it all off, no matter if it has more. What i want to do is display 10, then hit page "2" and it displays the next 10. I have tried several different things. I've tried the limit, I've tried looking at other message board sources (no, not the complex kind tlike phpBB. The simple kind), and just about everything. Right now, i have this code on my test script:


    PHP Code:
    else if (isset($letter)) 

    if (
    $letter == "number")
    {
    $sql="SELECT gameID, gamename, platform FROM games WHERE games.platform = 'gcn' AND games.gamename REGEXP '^[0-9]' ORDER BY gamename asc"

    else
    {
    $sql="SELECT gameID, gamename, platform FROM games WHERE games.platform = 'gcn' AND games.gamename LIKE '".$letter."%' ORDER BY gamename asc";
    }
    if (!isset(
    $page) || !is_numeric($page))
    {
      
    $page 1;    
    }

    $result=mysql_query($sql) or die ('Queryproblem'); 
    $numofitems mysql_num_rows($result);

    $display_top_nav 'true';

    if (
    $numofitems 2)
    {
    $prev $page-1;
    $next $page+1;
    $nop ceil($numofitems/2);
    echo 
    '<b><i>';
    echo 
    $page "<a href=\"{$_SERVER['REQUEST_URI']}&page=1\">First Page</a> | " '';
    echo 
    $page "<a href=\"{$_SERVER['REQUEST_URI']}&page={$prev}\">Previous Page</a> | " '';
    echo 
    "Page $page of $nop";
    echo 
    $page $nop-" | <a href=\"{$_SERVER['REQUEST_URI']}&page={$next}\">Next Page</a>" '';
    echo 
    $page $nop " | <a href=\"{$_SERVER['REQUEST_URI']}&page={$nop}\">Last Page</a>" '';
    echo 
    '</i></b>';}

    echo (
    '
    <table width="100%">
    <tr>
      <td class="B"><font size="7">' 
    $database '</font></td>
    </tr>
    <tr>
      <td class="A">Games by Alpha: ' 
    $letter '</td>
    </tr>
    '
    );
    $i 0;
    while (
    $row=mysql_fetch_assoc($result))

    $class $i&' class="shade"' ''
    echo (

    <tr' 
    $class '>
    <td>
    <b><a href="' 
    $_SERVER['PHP_SELF'] .'?game='$row['gameID'] .'">'$row['gamename'] .'</a></b></td></tr>
    '
    );
    $i++;
    }
    if (
    $numofitems 2)
        {
    $prev $page-1;
    $next $page+1;
    $nop ceil($numofitems/2);
          echo 
    '<tr><td align="center">Jump to Page: ';

          for (
    $i 1$i $nop+1$i++)
          {
            echo ${
    $page_var} != $i "<a href=\"{$_SERVER['REQUEST_URI']}&page={$i}\" class=\"menu\">$i</a>" $i;
            echo 
    $i $nop ' | ' '';
          }

          echo 
    '</td></tr>';
        } 



    echo (
    '

    </tr>
    </table>
    '
    );

    then at the top of the page:

    PHP Code:
    $page=$_GET['page']; 
    Change in this code: Instead of having 50 items, and limiting it to 10, I have g6 items and limiting it to 2 (or tring to, at leat). So, some of it works, like the page navigation (the first, next, prev, and last). No matter what page link I click, it gives me the same amount of items on the page. Although the Next, Prev, First, and Last links at the top change (like they should) iI get the same number of items. i have also notices that the number of pages that show up is however many item there are in total divided by 2. the problem is is that it doesn't limit it. And I can't use the MySQL LIMIT Query. Can somebody please check the WHOLE code inch-by-inch (cause I have, and I can't find anything) and tell me what to add, subtract, change, anything to make this working. If you don't understand what I am tring to do, then please, fell free to tell me. thanks.
    Last edited by XtremeGamer99; 10-20-2004 at 02:45 AM.

  • #2
    Regular Coder
    Join Date
    Aug 2004
    Location
    The US of A
    Posts
    767
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Try using:

    $page = ($page - 1) * 10;

    And in your SQL:
    LIMIT $page, 10

    I also reccomend reading this tutorial for pagination:
    Page Numbering With PHP And MySQL Results (PHPFreaks.com)

    I also uses this code for my pagination (I realise it is PsotGreSQL, but the PHP should work the same)

    PHP Code:
    $limit 20;
    $maxpages ceil($total $limit);    
    $page $_GET[page];
    if(empty(
    $page) || $page $maxpages) {$page 1;}
    $offset = ($page 1) * $limit
    In this case, $total is define by the num_rows function, and $offset would be the same as your $page.
    Last edited by Kurashu; 10-20-2004 at 03:46 AM. Reason: Added some code.

  • #3
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts


  •  

    Posting Permissions

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