Results 1 to 6 of 6
  1. #1
    Full Member dak142's Avatar
    Join Date
    January 17th, 2005
    I have written a php scrpit that displays 20 records using LIMIT 0,20 in my select statement. Short of creating new page for each additional 20 records, how can I automate this process?

    I see web pages that have " 1 | 2 | 3 | 4 | 5 " at the bottom of the page. Each link goes to the next 20 records. How can I do this using a PHP script? Any help would be appreciated!

  2. #2
    Full Member
    Join Date
    January 17th, 2005
    Probably best to write you own paging class for it if you are familiar with Object Oriented Programming.

    for an article on this.

    If you are using a database then it will be that bit easier and more efficient than if you are using a datafeed text/csv file.


  3. #3
    Join Date
    January 17th, 2005

    Do a search for "pagination" scripts. You'll find quite few out there.


  4. #4
    Join Date
    January 17th, 2005

    Here's a VERY basic one:

    if ($page != 1) {
    $pageprev = $page - 1;
    echo ("<< <a href=\"/[PAGE_NAME].php?page=$pageprev\">Previous</a> | ");
    $numofpages = ($totalrows / $limit);

    for ($i = 1; $i <= $numofpages; $i++) {
    if ($i == $page) {
    echo (" $i ");
    } else {
    echo (" <a href=\"/[PAGE_NAME].php?page=$i\">$i</a> ");
    if (($totalrows % $limit) > 0) {
    if ($i == $page) {
    echo (" $i ");
    } else {
    echo (" <a href=\"/[PAGE_NAME].php?page=$i\">$i</a> ");
    if (($totalrows - ($limit * $page)) > 0) {
    $pagenext = ($page + 1);
    echo (" | <a href=\"/[PAGE_NAME].php?page=$pagenext\">Next</a> >>");

    This will show as: << Previous |1|2|3|Next >>

    This should give you a starting point.


  5. #5
    Full Member dak142's Avatar
    Join Date
    January 17th, 2005
    Just need to vent, I feel like a PHP newbie! The concept make sense, the code looks good but man the results are all screwed up looking!

    The below code gives me 20 lines with just "n". That is comming from the echo's when the table is being built. I have been going crazy trying to figure out why it does that, and them processes the 20 records correctly, and then still does not produce the pagination links.

    Any PHP masters out there want to take a stab at this one?


    ###### MAIN RESULTS
    $link_id = $_GET['id'];
    ##connect to MySQL##
    $conn = $dbh=mysql_connect ('localhost', '****', '****') or die ('I cannot connect to the database because: ' . mysql_error());
    mysql_select_db ('*******');
    ################################### User ####### Passworw #######################

    ##Select data base to use##
    mysql_select_db('************',$conn) or die('Could not select database');
    ################# DATABASE NAME ##################################################

    $limit = 20;
    // Sets how many results shown per page

    $query_count = "SELECT count(*) FROM table";
    // Sets what we want to pull from the database
    // count(*) is better for large databases (thanks Greg!)

    $result_count = mysql_query($query_count);
    // Pulls what we want from the database

    $totalrows = mysql_num_rows($result_count);
    // This counts the number of users

    if(empty($page)){ // Checks if the $page variable is empty (not set)
    $page = 1; // If it is empty, we're on page 1

    $limitvalue = $page * $limit - ($limit);
    // Ex: (2 * 25) - 25 = 25 <- data starts at 25

    $query = "SELECT * FROM table LIMIT $limitvalue, $limit";
    $result = mysql_query($query) or die("Error: " . mysql_error());
    // Selects all the data from table.
    // mysql_error() will print an error if one occurs.

    /* Tip: The MySQL LIMIT value syntax is as follows:

    LIMIT $row_to_start_at, $how_many_rows_to_return


    if(mysql_num_rows($result) == 0){
    echo("Nothing to Display!");
    // This reads the number of rows returned
    // from the result above.

    /* Tip: You could probably use if($totalrows == 0) for the if statement;
    however, reading the actual $result from the data you'll be printing to the
    screen is more accurate, and is a surefire way of preventing certain errors. */

    $bgcolor = "#E0E0E0"; // light gray
    /* Sets up one of the background colors. The color stated here will be
    displayed second */

    // Just a simple table

    while($row = mysql_fetch_array($result)){
    /* This starts the loop (a while loop). What this does is returns a row of data
    to the $row array. Each time the loop restarts, the next row of data is used.
    So, each pass of the loop returns a different row of data. */

    // Start The Background Check
    if($bgcolor == "#E0E0E0"){
    $bgcolor = "#FFFFFF";
    $bgcolor = "#E0E0E0";
    /* Tip: The color you want to appear first on the list should be where the
    #FFFFFF is listed. What this script does is checks to see if #E0E0E0 was used
    last; if it was, then it'll use #FFFFFF. All you have to do is replace the
    colors of your choice. */

    echo("<tr bgcolor=".$bgcolor.">n<td>");
    // Here we start table row & table data
    // Make sure your bgcolor is the $bgcolor variable

    /* Tip: This is how we add the users field from our row. You can use any field
    from your row: all you do is include the field's name inbetween the array
    brackets. Ex: $row["field_name_here"] */

    // Here we end the one section of table data, and start another.

    // Prints the usersID field

    // Here we end the table data & table row

    } /* This closes the loop. Anything after this bracket will display after the
    data you've pulled from the database. */

    /* While we're at it, let's close the table tag--we don't want other data
    inside this table */

    if($page != 1){
    $pageprev = $page--;
    // Fancy way of subtracting 1 from $page

    echo("<a href=subjump.php&page=$pageprev\">PREV".$limit."</a>");
    /* Tip: It is a good idea NOT to use $PHP_SELF in this link. It may work,
    but to be 99.9% sure that it will, be sure to use the actual name of the file
    this script will be running on. Also, the adds a space to the end of
    PREV, and gives some room between the numbers. */
    // If we're on page 1, PREV is not a link

    $numofpages = $totalrows / $limit;
    /* We divide our total amount of rows (for example 102) by the limit (25). This

    will yield 4.08, which we can round down to 4. In the next few lines, we'll
    create 4 pages, and then check to see if we have extra rows remaining for a 5th
    page. */

    for($i = 1; $i <= $numofpages; $i++){
    /* This for loop will add 1 to $i at the end of each pass until $i is greater
    than $numofpages (4.08). */

    if($i == $page){
    echo("<a href=subjump.php&page=$i\">$i</a>");

    /* This if statement will not make the current page number available in
    link form. It will, however, make all other pages available in link form. */
    } // This ends the for loop

    if(($totalrows % $limit) != 0){
    /* The above statement is the key to knowing if there are remainders, and it's
    all because of the %. In PHP, C++, and other languages, the % is known as a
    Modulus. It returns the remainder after dividing two numbers. If there is no
    remainder, it returns zero. In our example, it will return 0.8 */

    if($i == $page){
    echo("<a href=subjump.php&page=$i\">$i</a>");
    /* This is the exact statement that turns pages into link form that is used

    above */
    } // Ends the if statement

    if(($totalrows - ($limit * $page)) > 0){
    /* This statement checks to see if there are more rows remaining, meaning there
    are pages in front of the current one. */

    $pagenext = $page++;
    // Fancy way of adding 1 to page

    echo("<a href=subjump.php?page=$pagenext\">NEXT".$limit."</a>");
    /* Since there are pages remaining, this outputs NEXT in link form. */
    /* If we're on the last page possible, NEXT will NOT be displayed in link
    form. */

    /* This line is not required, since MySQL will free the result after all
    scripts have finished executing; however, it's a nice little backup. */

    // The next line tells the server to stop parsing PHP

  6. #6
    Member mikew's Avatar
    Join Date
    January 17th, 2005
    southern California

    Are you sure you're not getting an empty result set? That wouldn't be a mysql_error. Make sure $limitvalue and $limit are set properly. Try echoing the value of mysql_num_rows() immediately following the select.

    I assume "Name" and "Thumbnail" are the correct, case-sensitive column names. If they aren't well...

    This shouldn't matter but you can try changing your while statement as follows:
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)){...

    For debugging you can always view your source and see if it looks funny somewheres. I've spotted loads of errors this way.

    You can also debug by commenting out the entire loop and replacing it with a single line, something like:
    echo "Name = " . $row["Name"] . " Thumbnail = " . $row["Thumbnail"];
    and see if you get what you expect.

    Hope this helps, good luck

  7. Newsletter Signup

+ Reply to Thread

Similar Threads

  1. bug on multiple index pages?
    By fabio in forum WebMerge (
    Replies: 0
    Last Post: June 8th, 2012, 07:21 PM
  2. Specials/Coupons on multiple pages - Do not want index by SE
    By kse in forum Programming / Datafeeds / Tools
    Replies: 2
    Last Post: December 18th, 2007, 03:12 PM
  3. Multiple index & record pages
    By likemynick in forum WebMerge (
    Replies: 1
    Last Post: February 21st, 2007, 12:03 PM
  4. Non-sequential linking of multiple index pages
    By EdwinM in forum WebMerge (
    Replies: 2
    Last Post: December 9th, 2006, 11:45 PM
  5. Making an index page of index pages
    By Pierre (aka Terdef) in forum WebMerge (
    Replies: 0
    Last Post: June 30th, 2005, 04:24 AM

Posting Permissions

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