PHP Page 2 - Creating a Searchable Inventory System: Retrieving and Managing Search Results (continued) |
You may have noticed that we have introduced into our application several “get” functions, without having ever used their corresponding “set” functions. We’re now ready to implement those “set” functions, and we will do so by creating a listener method of sorts. Ideally, our search class should itself check when drawing a new page to see if any new search criteria have been submitted. In the event that something has been submitted, it then needs to update the appropriate internal variables so that our “get_search_results” functions properly. This can be done through the use of a listener method, although that method must still be called from within our “index.php” file. To start, we will need to make two minor updates to our “index.php” file. First, we will need to instantiate our search class at the very top of the page, directly under our initial include statement, as shown below. Then we will want to immediately call a method within that class, “init_search” which we will be creating momentarily. <?php @ include_once 'inc.core.php'; ?> Lastly, to keep things clean, we will want to add a hidden input field to our search form, directly below the opening form tag: <form name="search_form" action="index.php" method="post"> This field, “search_flag”, will enable us to quickly and easy check to determine when a new set of search criteria are submitted. As you may have guessed, the function that will check for this is “init_search”, and it will check some other things as well. Let’s create it now within our “search.php” page, then we’ll take a closer look at it: function init_search() { # check for any newly-submitted search details # re-set search query # walk through search array, and check for # properly format field name before # store field value in SESSION for # if a value has been set, append } # END if field value } # END foreach array field # re-set current search page to first upon new search # if ORDER BY information has been provided, set it # if field has been specified as DESC, set ASC # store newly selected field # re-set current search page to first upon new # if new LIMIT (page) has been provided, set it in $this->set_limit_start_value( $_GET[ $GLOBALS } # END if new search, order by, or limit info # retrieve count of all records for current search # if results have been found (aka Query isn't broken) # if no results were found (broken query) } # END if valid query } # END init_search() This is an important function, but at heart it’s relatively simple. It does four basic things: store newly submitted search criteria, store user “sort-by” actions, store the current page of records chosen by the user, and store the total number of records found using the current search criteria.
blog comments powered by Disqus |
|
|
|
|
|
|
|