PHP & MySQL: Displaying query if limit over number on a new page

Hello,

I want to display a certain query on another page, if the query exceeds the amount of the “LIMIT” of a number. The actual algorithm is probably easier than I think, and it’s not the first time for me doing database based queries, though so far that’s the only thing that got me. :lol:

However, this is my code:


    <?php
        
     ob_start();
     require('php/connect.php');

     $output    = mysql_query("SELECT * FROM applies LIMIT 5");

            echo"<table class='aplc'>
                  <tr>
                   <th class='aplc2th'>ID</th>
                   <th class='aplcth'>Nickname</th>
                   <th class='aplc2th'>Partner</th>
                   <th class='aplcth'>Date</th>
                   <th class='aplc2th'>IP</th>
                   <th class='aplcth'>Check</th>
                  </tr>
                 ";

       if (mysql_num_rows($output) >=1) {
          while ($row = mysql_fetch_assoc($output)) {

             $id           = $row['id'];
             $nick         = $row['nick'];
             $partner      = $row['partner'];
             $datetime     = $row['wedding_date'];
             $ip           = $row['ip'];
             $checkifmodr  = $row['check_if_moderated'];


             echo"
                  <tr>
                    <td class='aplctd'>$id</td>
                    <td class='aplc2td'>$nick</td>
                    <td class='aplctd'>$partner</td>
                    <td class='aplc2td'>$datetime</td>
                    <td class='aplctd'>$ip</td>
                    <td class='aplc2td'>$checkifmodr</td>
                  </tr>";

          }

             echo" 
               </table><br>";
        }
      

     ?>


I don’t want it too fancy, I need to figure out how to do it on my own, I just want it simple, if there’s someone that could me accomplish this, I’d be very glad. (:

Regards

Not exactly sure what you want to do here :frowning:

Do you want to paginate the results from your query or do you want to open a new window to display the query results or is it something else you want to do?

Paginating the results could be the right description for that, yes.

I’d be surprised if this type of question hasn’t come up in these forums before so I just did a search for “Pagination” in the thread titles and came up with a few hits. This thread seems to have some interesting code you could look at.

It can be simplier, and for me it’s not simple for educational purposes concerning my code.

ok then. The basic concept behind paginating results is retrieve just the records you need for that page number by

  1. calculate how many pages you will need by dividing the total number of records in the db table by the number of records you want to display per page and round up to nearest integer.

  2. calculate the offset value for your query for a particular page.

  3. retrieve the records for a page number using the offset for that page and the limit value (number of records per page)

  4. store the current page the user is on either in a session variable (my preference) or cookie.

  5. set up the required page number links from the number of pages you calculated in step 1 and previous, next, first and last links.

The above is just a general overall description of what needs to be done as paginating search results successfully is not a trivial task, but it’s not too difficult either if you have a reasonable grasp of php, sql and html.

We even have an article here taking you through all about it.
SitePoint article: Perfect PHP Pagination

Sounds good, but isn’t good. I found a way better example, an example that can “also be understood more logicially”, and in a way that everyone writes code. Or do you think everyone is gonna use classes, - why do you think the article doesn’t have the entire class on their website, but as a download zip file? Advertisement rules the world

I prefer to use a pagination class because I can then very easily bolt it on to other applications without having to change any code. The advantage of classes is that they make it easy to write standalone reusable and portable code.

But each to their own on how to do it :slight_smile: