HomePHP Page 2 - Creating a Searchable Inventory System: Retrieving and Managing Search Results
Methods of Storing Search Criteria - PHP
In this third article in our series, we will look at the type of functions for which we need to use our search information, then take a look at retrieving records based on various search criteria.
In order to allow the greatest flexibility for our search tool, we will want to store each bit of information entered as a separate entity so that we may reassemble that information in a variety of ways. We took a brief look at this when we spoke about the helper functions “set_field_session_value” and “get_field_session_value”.
By using these functions to store the user-submitted value for each individual search field separately, we were able to create a process by which each field on our search form would be pre-populated with the most recent search value provided. In other words, if the user chose the “Motherboards” parts category when running a search, then that category will appear as pre-selected each time the search form is redrawn. This will continue until another value is provided or the browser is closed.
But it doesn’t stop there. There are other elements that go into our search query that we will also wish to store separately, such as the field name and direction by which records are to be sorted, or the number of records which we are currently viewing (ex. records 10-20). Only by storing each of these components separately are we allowed complete flexibility within our search tool.
So which elements do we need to store exactly? And how will we access them? First, let’s start by adding the following helper-functions to our “search.php” file.
function get_limit_start_value() { return ( !empty( $_SESSION ['db_limit_start_value'] ) ) ? $_SESSION ['db_limit_start_value'] : '0'; } # END get_limit_start_value()
function get_order_by_direction() { return ( !empty( $_SESSION ['db_order_by_direction'] ) ) ? $_SESSION ['db_order_by_direction'] : $GLOBALS ['db_default_order_by_direction']; } # END get_order_by_direction()
function get_order_by_field() { return ( !empty( $_SESSION ['db_order_by_field'] ) ) ? $_SESSION ['db_order_by_field'] : $GLOBALS ['db_default_order_by_field']; } # END get_order_by_field()
function get_where_constraints_value() { return $_SESSION['db_query_value']; } # END get_where_constraints_value ()
function set_limit_start_value ( $p_limit_start_value ) { $_SESSION['db_limit_start_value'] = $p_limit_start_value; } # END set_limit_start_value()
function set_order_by_direction ( $p_order_by_direction ) { $_SESSION['db_order_by_direction'] = $p_order_by_direction; } # END set_order_by_direction()
function set_order_by_field ( $p_order_by_field ) { $_SESSION['db_order_by_field'] = $p_order_by_field; } # END set_order_by_field()
function set_where_constraints_value ( $p_query_value ) { $_SESSION['db_query_value'] = $p_query_value; } # END set_where_constraints_value()
Those of you familiar with SQL syntax will probably recognize the item that each function above is meant to store. “set_order_by_field”, for example, will store the name of the field by which we wish to order our resulting records, and “get_order_by_field” will retrieve that field name for us from the SESSION at a later time.
A careful look will also reveal that we have introduced a couple of new GLOBAL variables within these functions: “db_default_order_by_direction” and “db_default_order_by_field”. These variables, set in our “inc.conf.php” file, will allow us to choose a default sort direction for search results. In other words, when a user runs a search for the first time, we can control which order the results are initially presented in by editing these two values. Let’s go ahead and add them to our configuration file:
# define default ORDER BY portion of the query # NOTE: we're setting things to arrange from newest to oldest $GLOBALS['db_default_order_by_field'] = 'pc_parts.id'; $GLOBALS['db_default_order_by_direction'] = 'DESC';
By adding the above lines of code to our configuration file, we are telling our script to sort parts by the order they were added to the database, with the newest records being displayed first. This is a good default sort mode for users who visit our inventory on a regular basis, because they are shown the newest parts first rather than the same older parts over and over again.