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 10 of 10

Thread: Php Pagination

  1. #1
    Regular Coder
    Join Date
    Sep 2008
    Posts
    697
    Thanks
    8
    Thanked 17 Times in 16 Posts

    Php Pagination

    I am getting this error with a script:

    Code:
    Parse error: syntax error, unexpected T_IF in /home/brian/public_html/pages.php on line 5
    PHP Code:
    <?php
    // Connects to your Database
    include("inc/config.php");(
    //This checks to see if there is a page number. If not, it will set it to page 1
    if (!(isset($pagenum)))
    {
    $pagenum 1;
    }

    //Here we count the number of results
    //Edit $data to be your query
    $data mysql_query("SELECT * FROM accounts") or die(mysql_error());
    $rows mysql_num_rows($data);

    //This is the number of results displayed per page
    $page_rows 4;

    //This tells us the page number of our last page
    $last ceil($rows/$page_rows);

    //this makes sure the page number isn't below one, or more than our maximum pages
    if ($pagenum 1)
    {
    $pagenum 1;
    }
    elseif (
    $pagenum $last)
    {
    $pagenum $last;
    }

    //This sets the range to display in our query
    $max 'limit ' .($pagenum 1) * $page_rows .',' .$page_rows

    //This is your query again, the same one... the only difference is we add $max into it
    $data_p mysql_query("SELECT * FROM accounts $max") or die(mysql_error());

    //This is where you display your query results
    while($info mysql_fetch_array$data_p ))
    {
    Print 
    $info['Name'];
    echo 
    "<br>";
    }
    echo 
    "<p>";

    // This shows the user what page they are on, and the total number of pages
    echo " --Page $pagenum of $last-- <p>";

    // First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
    if ($pagenum == 1)
    {
    }
    else
    {
    echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
    echo 
    " ";
    $previous $pagenum-1;
    echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
    }

    //just a spacer
    echo " ---- ";

    //This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
    if ($pagenum == $last)
    {
    }
    else {
    $next $pagenum+1;
    echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
    echo 
    " ";
    echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
    }
    ?>
    Anyone know why?

    This is the structure of my table:
    Code:
    +---------+------------+-----------+
    | Table Name: accounts             |
    +---------+------------+-----------+
    |    id   |  username  |  password |
    +---------+------------+-----------+
    |       1 | john12     | thepass   |
    |       2 | catman17   | cat       |
    +---------+------------+-----------+
    Last edited by bucket; 11-02-2009 at 05:14 PM.

  • #2
    Regular Coder
    Join Date
    Sep 2008
    Posts
    697
    Thanks
    8
    Thanked 17 Times in 16 Posts
    I have fixed it:

    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    ini_set('display_errors''1');
    include(
    "inc/config.php");
    if (!(isset(
    $pagenum)));
    {
    $pagenum 1;
    }
    $data mysql_query("SELECT * FROM accounts") or die(mysql_error());
    $rows mysql_num_rows($data);
    $page_rows 4;
    $last ceil($rows/$page_rows);
    if (
    $pagenum 1)
    {
    $pagenum 1;
    }
    elseif (
    $pagenum $last)
    {
    $pagenum $last;
    }
    $max 'limit ' .($pagenum 1) * $page_rows .',' .$page_rows
    $data_p mysql_query("SELECT * FROM accounts $max") or die(mysql_error());
    while(
    $info mysql_fetch_array$data_p ))
    {
    Print 
    $info['username'];
    Print 
    $info['password'];
    echo 
    "<br>";
    }
    echo 
    "<p>";
    echo 
    " --Page $pagenum of $last-- <p>";
    if (
    $pagenum == 1)
    {
    }
    else
    {
    echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
    echo 
    " ";
    $previous $pagenum-1;
    echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
    }
    echo 
    " ---- ";
    if (
    $pagenum == $last)
    {
    }
    else {
    $next $pagenum+1;
    echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
    echo 
    " ";
    echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
    }
    ?>
    Now for some reason it is not going to the next page.

  • #3
    Regular Coder
    Join Date
    Sep 2008
    Posts
    697
    Thanks
    8
    Thanked 17 Times in 16 Posts
    It is only showing the first 4 rows and it wont show the rest when I click the next or last button.

  • #4
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    If you can wait, I think ill create a plug in Pagination script tomoz mornin that anyone can use.
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #5
    Regular Coder
    Join Date
    Sep 2008
    Posts
    697
    Thanks
    8
    Thanked 17 Times in 16 Posts
    Well I have fixed it, but I was wondering if you can just help me.

    I really need this today and if its something simple to fix.

    Another person said do this:

    do a requery... add "LIMIT" on your sql statement.
    I have no idea what that means.

  • #6
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    You have the limit set. from first glance it seems fishy as i cant quite understand the crack

    PHP Code:
    $max 'limit ' .($pagenum 1) * $page_rows .',' .$page_rows
    so if pagenum = 3
    and page rows = 10

    result would be
    LIMIT 20, 10

    PHP Code:
    $fst $pagenum $page_rows;
    $max 'LIMIT ' .$fst .',' .$fst+$page_rows
    i think...

    So then be LIMIT 30, 40
    Last edited by Phil Jackson; 11-02-2009 at 07:20 PM.
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #7
    Regular Coder
    Join Date
    Sep 2008
    Posts
    697
    Thanks
    8
    Thanked 17 Times in 16 Posts
    So the updated code is:

    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    ini_set('display_errors''1');
    include(
    "inc/config.php");
    if (!(isset(
    $pagenum)));
    {
    $pagenum 1;
    }
    $data mysql_query("SELECT * FROM accounts") or die(mysql_error());
    $rows mysql_num_rows($data);
    $page_rows 4;
    $last ceil($rows/$page_rows);
    if (
    $pagenum 1)
    {
    $pagenum 1;
    }
    elseif (
    $pagenum $last)
    {
    $pagenum $last;
    }

    $fst $pagenum $page_rows;
    $max 'LIMIT ' .$fst .',' .$fst+$page_rows;  


    $data_p mysql_query("SELECT * FROM accounts $max") or die(mysql_error());
    while(
    $info mysql_fetch_array$data_p ))
    {
    Print 
    $info['username'];
    Print 
    $info['password'];
    echo 
    "<br>";
    }
    echo 
    "<p>";
    echo 
    " --Page $pagenum of $last-- <p>";
    if (
    $pagenum == 1)
    {
    }
    else
    {
    echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
    echo 
    " ";
    $previous $pagenum-1;
    echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
    }
    echo 
    " ---- ";
    if (
    $pagenum == $last)
    {
    }
    else {
    $next $pagenum+1;
    echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
    echo 
    " ";
    echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
    }
    ?>
    I changed
    PHP Code:
     $max 'limit ' .($pagenum 1) * $page_rows .',' .$page_rows
    to

    PHP Code:
    $fst $pagenum $page_rows;
    $max 'LIMIT ' .$fst .',' .$fst+$page_rows
    Is that right?

    I now have this error:
    Code:
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '4' at line 1
    Last edited by bucket; 11-02-2009 at 07:31 PM.

  • #8
    Regular Coder
    Join Date
    Sep 2008
    Posts
    697
    Thanks
    8
    Thanked 17 Times in 16 Posts
    I have fixed it:

    PHP Code:
    <?php
    error_reporting
    (E_ALL);
    ini_set('display_errors''1');
    include(
    "inc/config.php");

    $pagenum = (isset($_GET['pagenum'])) ? (int) $_GET['pagenum'] : 1;

    $data mysql_query("SELECT * FROM accounts") or die(mysql_error());
    $rows mysql_num_rows($data);
    $page_rows 4;
    $last ceil($rows/$page_rows);
    if (
    $pagenum 1)
    {
    $pagenum 1;
    }
    elseif (
    $pagenum $last)
    {
    $pagenum $last;
    }
    $max 'limit ' .($pagenum 1) * $page_rows .',' .$page_rows;
    $data_p mysql_query("SELECT * FROM accounts $max") or die(mysql_error());
    while(
    $info mysql_fetch_array$data_p ))
    {
    echo 
    "Username: ";
    Print 
    $info['username'];
    echo 
    "<br>";
    echo 
    "Password: ";
    Print 
    $info['password'];
    echo 
    "<br>----";
    echo 
    "<br>";
    }
    echo 
    "<p>";
    echo 
    " --Page $pagenum of $last-- <p>";
    if (
    $pagenum == 1)
    {
    }
    else
    {
    echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
    echo 
    " ";
    $previous $pagenum-1;
    echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
    }
    echo 
    " ---- ";
    if (
    $pagenum == $last)
    {
    }
    else {
    $next $pagenum+1;
    echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
    echo 
    " ";
    echo 
    " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
    }
    ?>


    ----

    Now I need help with another thing, How would I do it so instead of it posting it like this when im at the first page:

    ---- Next -> Last ->>

    To like this:

    | 1 | 2 | 3| Last ->>

    Like it adds a number based on how many pages, its like instead of using the Next -> thing clicking over and over again to get to like page 5.

    Same thing with the rest of the pages:

    <<-First | 1 | 2 | 3| Last ->>

    And the last page:

    <<-First | 1 | 2 | 3|

    Does anyone know how to do this? I want number pages instead of clicking to get to the next page or the page before.

  • #9
    met
    met is offline
    Regular Coder
    Join Date
    Oct 2009
    Location
    United Kingdom
    Posts
    728
    Thanks
    4
    Thanked 119 Times in 119 Posts
    consider using LIMIT and OFFSET

    LIMIT=5, OFFSET=10 (show 5 records, starting from the 10th)

    increment offset by LIMIT each time.

    As for the URLs, divide the total number of rows by the amount you want to show on each page (100records/20 per page==5 pages)

    then do a loop

    PHP Code:
    $offset=0;
    for(
    $i=1;$i<=5;$i++) { echo '<a href="?offset='.$offset '">'.$i.</a>;
    $offset=$offset+20//20=data per page
    }
    // do query 
    Last edited by met; 11-02-2009 at 08:30 PM.

  • #10
    Regular Coder
    Join Date
    Sep 2008
    Posts
    697
    Thanks
    8
    Thanked 17 Times in 16 Posts
    Where would I add that code into my script?


  •  

    Posting Permissions

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