SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Mar 2012
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with pagination

    Good Morning Everyone,

    I have a problem, I am trying to display a large number results using pagination. Here is my code so far:


    <?php
    $con = mysql_connect("localhost","user","pass);
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
    echo 'Connected successfully';

    mysql_select_db("db", $con);

    $result = mysql_query("SELECT * FROM report ORDER BY report_id");
    echo "<div style='float: left; width: 780px; margin-top: 20px;'>";
    while($row = mysql_fetch_array($result))
    {
    echo $row['city'];
    echo " " . $row['address'];
    echo " " . $row['mda'];
    echo "<br />";
    }
    echo "</div>";
    mysql_close($con);
    ?>

    How do I go about it ?

    Regards
    Last edited by guido2004; Mar 7, 2012 at 04:30. Reason: corrected post title

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Start by reading about how other people go about it php pagination tutorials.

    Essentially, if you are using Mysql you will make use of its LIMIT clause which permits you to get the next 5 results starting at the 20th with the likes of LIMIT 20, 5.

    Depending on your needs you may have to get the total number of results to work out how many "pages" will be required - maintain state between requests and so on.

    You can get completely lost in trying to satisfy all the complexities paging requires - if you've found me 500 results and my target is not on page 1 I'd prefer to see more work put into showing me how I could filter those results down further ... and maybe a "next >" button so I could go to page 2 if I really wanted to... that may not be what your client wants though.

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,327
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Cups View Post
    ...get the next 5 results starting at the 20th with the likes of LIMIT 20, 5
    that LIMIT would start at the 21st row

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •