PHP DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


Creating Dynamic Next / Previous Buttons with PHP and MySQL
Pages: 1, 2

The magic code arrives

Now that we understand what a good navigation class needs to have, let's start coding it. Some variables are going to need to be available so the user can choose how many records to show in each screen, for instance. The user also needs to be able to change the default values of the "Next page" and "Previous page" strings to whatever they want. They can also choose whether to show the "Next page" and "Previous page" strings even when they are not needed, i.e., on the first and last screens of your script.



The variables are:

<?php
  var $numrowsperpage = 10;
  var $str_previous = "Previous page";
  var $str_next = "Next page";

  // These two variables are used internally 
  // by the class' functions
  var $file;
  var $total_records;
?>

I split the class into three functions to make the code easier to maintain and read in the long run. It might not be very intuitive at first, but after a couple of minutes of studying the code, you will recognize its simplicity. Thank you, by the way ;-)

Let's start with the execute() function :

<?php
  function execute($sql, $db, $type = "mysql") {
    // global variables needed by the function
    global $total_records, $row, $numtoshow;

    // number of records to show at a time
    $numtoshow = $this->numrowsperpage;
    // $row is actually the number of the row of 
    // records (the page number)
    if (!isset($row)) $row = 0;
    // the record start number for the SQL query
    $start = $row * $numtoshow;
    // check the database type
    if ($type == "mysql") {
      $result = mysql_query($sql, $db);
      $total_records = mysql_num_rows($result);
      $sql .= " LIMIT $start, $numtoshow";
      $result = mysql_query($sql, $db);
    } elseif ($type == "pgsql") {
      $result = pg_Exec($db, $sql);
      $total_records = pg_NumRows($result);
      $sql .= " LIMIT $numtoshow, $start";
      $result = pg_Exec($db, $sql);
    }
    // returns the result set so the user 
    // can handle the data
    return $result;
  }
?>

As you can note, this function executes the SQL query on the selected database. The default type of database is MySQL, but that can be changed as well. It first runs the full query, that is, the query returning all the rows, so we can always know the total number of records, and the current offset for the pages.

Building the navigation link array

Since we already have executed our query, the navigation links can be created. This is an especially cool feature, since instead of printing out all the links in some pre-defined format, the class is actually returning an array with all the links (i.e., $array[0] holds <A HREF='example.php?row=0'>1</A>). This enables users to print the navigation links in whatever fashion they choose, adding even more customizability to the class.

Let's look at the contents of the getlinks() function -- you can view it here.

What it does is calculate the total number of pages that can be built according to the number of records per page the user selected. Then, it starts creating an array of links to return to the calling script. This is especially important, since a lot of user-defined options are available in this stage: whether to show page numbering links or not, whether to show the "Next" and "Previous" links or not, and whether to show the "Next" and "Previous" strings when those links are not needed.

The first element of this array is always the "Previous" link/string. This means that $array[0] is always <A HREF='example.php?row=X'>Previous</A>, the string "Previous" or just blank. The last element of the array is the "Next" link/string, with the same possibilities as the first one. Finally, all the elements between the first and the last elements are the page numbering links/strings.

Since the class doesn't affect how the script outputs the navigation link information in any way, the user is always free to customize the returned array to the page design.

More on building the magic array

Okay, by this point you may be a little bit confused, or even overwhelmed by so much information at once. The class can seem complex at first, but let's keep on analyzing it.

The second assignment of the getlinks() function is to actually build the GET type URL that is going to be returned to the calling script. It is completely dynamic, so the options that the user chose in the previous script are remembered on the next screen.

View contents of build_geturl() function here.

All this function does is get the filename of the current script, or the requested URL and assign it to a class function for later use. It then checks for the form method type and builds a GET-type URL to be appended later to the navigation link's URL. That's how the class always knows and remembers the URL and variables passed to it. Sweet, huh? :)

You can download an example of how to use this class, and of course, the class itself from these links -- download them as text files, and then rename them so they have a .php extension: example.txt and navbar.txt.

That was it. You can use this class on your own projects as much as you want, but please leave the credits on it. Also, if you want, send me an e-mail saying that you liked it and you are using it for your projects. It will make me happy.

Joao Prado Maia is a web developer living in Houston with more than four years of experience developing web-based applications and loves learning new technologies and programming languages.


Discuss this article in the O'Reilly Network PHP Forum.

Return to the PHP DevCenter.

 




Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!


Sponsored by: