Home arrow PHP arrow Page 2 - Creating a Searchable Inventory System: Retrieving and Managing Search Results (continued)

Automatic Search Listener - PHP

In this article, we will set up an automatic search listener to execute the code for our searchable inventory system. With this final piece, we will assemble all of the parts of our application into a working search tool. It will allow us to search for records using any combination of the search forms, as well as sort those records by almost any of the resulting column headers.

  1. Creating a Searchable Inventory System: Retrieving and Managing Search Results (continued)
  2. Automatic Search Listener
  3. Explaining the Function
  4. Displaying Search Results
By: Brian Vaughn
Rating: starstarstarstarstar / 4
November 29, 2005

print this article



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'; ?>
<?php $search = new search(); ?>
<?php $search->init_search(); ?>

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">
      <input type="hidden" name="search_flag" value="1">

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
            if ( isset( $_POST['search_flag'] ) ) {           

                  # re-set search query
( NULL );                 

                  # walk through search array, and check for
submitted values
                  if ( is_array( $GLOBALS['search_array'] ) ) {
                        foreach( $GLOBALS['search_array'] as
$l_field_name => $l_field_array ) {                       

                              # properly format field name before
                              $l_search_field_name    =
str_replace( ' ', '_',  $l_field_name );                             

                              # store field value in SESSION for
( $l_field_name, $_POST
[ $l_search_field_name ] );                       

                              # if a value has been set, append
it to query string
                              if ( !empty( $_POST
[ $l_search_field_name ] ) ) {
>set_where_constraints_value( $this->get_where_constraints_value
() . ' AND ' . str_replace( '<<field>>', mysql_escape_string
( $_POST[ $l_search_field_name ] ), $l_field_array
['search_pattern'] ) );                 

                              } # END if field value

                        } # END foreach array field
                  } # END if search array found                 

                  # re-set current search page to first upon new search
                  $this->set_limit_start_value( 0 );           

            # if ORDER BY information has been provided, set it
in query
            } else if ( !empty( $_GET[ $GLOBALS
['db_order_by_field_name'] ] ) ) {
                  $f_db_order_by_field    = $_GET[ $GLOBALS
['db_order_by_field_name'] ];           

                  # if field has been specified as DESC, set ASC
                  # if field has been specified ASC (or not at
all) set DESC
                  $this->set_order_by_direction( ( $this-
>get_order_by_field() != $f_db_order_by_field || $this-
>get_order_by_direction() == 'DESC' ) ? 'ASC' :
'DESC' );                       

                  # store newly selected field
( $f_db_order_by_field );                 

                  # re-set current search page to first upon new
                  $this->set_limit_start_value( 0 );           

            # if new LIMIT (page) has been provided, set it in
            } else if ( isset( $_GET[ $GLOBALS
['db_limit_start_name'] ] ) ) {

                  $this->set_limit_start_value( $_GET[ $GLOBALS
['db_limit_start_name'] ] );           

            } # END if new search, order by, or limit info

            # retrieve count of all records for current search
            $db_result  = mysql_query( $GLOBALS
['db_base_count'] . ' ' . $GLOBALS['db_base_from'] . ' ' .
$GLOBALS['db_base_where'] . ' ' . $this-
>get_where_constraints_value() );           

            # if results have been found (aka Query isn't broken)
            if ( @ mysql_num_rows( $db_result ) > 0 ) {
                  $f_record_count   = mysql_fetch_assoc
( $db_result );
                  $this->set_total_record_count( $f_record_count
['record_count'] );           

            # if no results were found (broken query)
            } else {
                  $this->set_total_record_count( 0 );           

            } # 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.

>>> More PHP Articles          >>> More By Brian Vaughn

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: