Assuming a SELECT query is already performed, the following code can be used to display rows corresponding to the results of the query:
For a query that has a lot of rows, it may not be effective to display all rows on the same page. This problem can be fixed by the use of the LIMIT clause in a SELECT statement.
To use this feature, the PHP script needs to know the number of entries to display on a single page. This can be a fixed number, but it may be best to make it a variable so that browsers with a larger display can display more rows. The following code may be used to specify the number of rows.
To perform paging, you will also need to track the start row of the current page. In a form, this is best done using a “hidden” type input element. Outside of a form, this has to be a parameter specified in the “href” attribute.
The script should always check to see if such a field exists. If not, the default should be 0 (the LIMIT clause is zero oriented). The
Next, we also need to generate the navigation links/controls. To generate such links/controls, it is important to first know the number of rows in a query.
One may be tempted to use the following approach:
This approach works, but it also has a performance issue. PHP actually needs to get the entire result of the query to do this counting. This means that if the result ends up with thousands of rows, all these rows need to be communicated from the SQL server back to the server running the PHP script first.
A better approach is to use SQL to do the counting, as demonstrated in the following approach:
Because this approach is generally very useful, it makes sense to make it a function.
Once we know the total number of rows, we can generate navigational links. Most navigation links has a “previous” and a “’next’. Some also includes a few pages around the current page. For simplicity, we will just generate links to all pages.
Note how this code does not actually encode the start row. This allows the PHP code to compute and validate the start row when it is time to display content of a page like follows:
With the start row computed (it is zero oriented), we can now specify the query string.
The rest of the code to actually take each row to display is the same as before.
In certain queries, it is important to index each row so that it can be selected and identified. This can be for the purposes of editing a single entry, or deleted entries that are selected (checkboxes).
Multi-delete can be supported by the use of a checkbox per row. The code to generate a checkbox may look like the following:
This code assumes the name of the checkbox is stored in the variable “strMultiSelect” and the unique ID of each row is the first field. This approach requires a submit button that specifies the action of “delete selected”.
The matching code to act on the submit button click may look like the following:
The code to generate an anchor to edit a row may look like the following:
The matching code to generate the form with items pre-filled may look like the following:
When the submit button of the form is clicked, whichever PHP is to process it may have the following code: