Home arrow PHP arrow Page 3 - Creating a Searchable Inventory System: Setting Up Your Database and User Interface (continued)

Our First Helper Search Function - PHP

In this second part of an article about setting up a searchable inventory system, we will cover our helper Search class, and some of the things this class will need to be able to do.

TABLE OF CONTENTS:
  1. Creating a Searchable Inventory System: Setting Up Your Database and User Interface (continued)
  2. Helper Search Class
  3. Our First Helper Search Function
  4. Dynamic SELECT Menus
By: Brian Vaughn
Rating: starstarstarstarstar / 9
November 15, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Since any web search begins with a search form, it is appropriate that our helper Search class begins with the “get_search_field” function. We’ve already defined all of the information required to create that function within our “inc.conf.php” file, so let’s take a look at the code and how it will work:

      function get_search_field( $p_search_field_name ) {
            $r_html     = NULL;

            # retrieve search field information from GLOBALS
array
            $f_search_array   = $GLOBALS['search_array']
[ $p_search_field_name ];
           

            # check session for pre-set search field value
            $f_input_value    = $this->get_field_session_value
( $p_search_field_name );           

            # retrieve any additional pre-set HTML attributes
            $f_html_attributes      = $f_search_array
['html_attributes'];           

            # properly format field name before continuing
            $f_search_field_name    = str_replace( ' ', '_',
$p_search_field_name );       

            # if field is of type select, then return a SELECT
input field with all appropriate options
            if ( $f_search_array['field_type'] == 'select' )
{           

                  # open select tag
                  $r_html     .= "<SELECT
NAME='$f_search_field_name' ID='$f_search_field_name'
$f_html_attributes /> \r\n";                 

                  # append (empty) "Any" option
                  $r_html     .= "<OPTION value=''>Any</OPTION>
\r\n";                 

                  # retrieve all appropriate OPTIONs from DB
using specified query
                  # NOTE: replace placeholder "<<field>>" with
appropriate parent-select-menu value if applicable
                  $db_result  = mysql_query( str_replace
( '<<field>>', $this->get_field_session_value( $f_search_array
['parent_menu'] ), $f_search_array['populate_query'] ) );
                  if ( @ mysql_num_rows( $db_result ) ) {
                        while ( $db_record = mysql_fetch_assoc
( $db_result ) ) {           

                              # retrieve SQL values
                              $l_option_value         =
$db_record['value'];
                              $l_option_display = $db_record
['display'];                             

                              # check to see if current option
has been selected
                              $l_selected = ( $l_option_value ==
$f_input_value ) ? 'selected' : NULL;           

                              # append OPTION tag
                              $r_html     .= "<OPTION value='$l_option_value' $l_selected>$l_option_display</OPTION>
\r\n";                       

                        } # END for each result                 

                  } # END if results found                 

                  # close select tag
                  $r_html     .= "</SELECT> \r\n";           

            # if field is of type text, then simply return a TEXT
input field
            } else {           

                  # return a text input field
                  $r_html     .= "<INPUT TYPE='text'
NAME='$f_search_field_name' ID='$f_search_field_name'
VALUE='$f_input_value' $f_html_attributes />";

           } # EBD 'field_type' check           

            # return HTML
            return $r_html;           

      } # END get_search_field()

As you can see, we not only completed the “get_search_field” function, but we also introduced an additional helper function, “get_field_session_value”. Let’s take a quick look at it, and its matching “set_field_session_value” function, before continuing:     

      function get_field_session_value( $p_search_field_name ) {
            return $_SESSION[ 'search_' . $p_search_field_name ];
      } # END get_field_session_value()     

      function set_field_session_value( $p_search_field_name,
$p_search_field_value ) {
            $_SESSION[ 'search_' . $p_search_field_name ]   =
$p_search_field_value;
      } # END set_field_session_value()

The purpose of the “get_field_session_value” function is simple: to retrieve a user-defined value for the particular field of choice, if any, from the SESSION. As the user runs a search, the value specified for each search field will be stored within the SESSION (using “set_field_session_value”). Then as the page is re-drawn PHP will pre-select the value(s) previously provided by the user. This step is not necessary, but will help to avoid any confusion in the mind of the user as he or she uses our search form.

Now back to the “get_search_field” function. As you can see, this function is comprised mostly of comments, so it should be relatively self explanatory. First we retrieve the array from the configuration file, then using the values we have defined for each menu object, we create the appropriate field. If the field should be a SELECT menu, we also retrieve a list of appropriate OPTION tags using the query specified in the configuration array.

There are only a couple of items which may cause confusion. The first deals with the SELECT menu query, “pre_populate”. In order to keep our helper function(s) as simple as possible, this query must retrieve its resulting SQL information in the form of two fields: “value” and “display”. The “value” field is used to populate the “value” attribute of our HTML OPTION tag, while the “display” field is used to actually display the information to the user. In other words, if the query retrieves a record with the display field of “Bob” and a value of “3”, “Bob” is what is shown to the user but “3” is the value submitted when a new search is triggered.

Secondly, two of our fields specify a “parent_menu” name, “Categories”. As our helper function retrieves the SQL query used to populate these search menus with a list of OPTION tags, it also checked the “parent_menu” field’s session value (using the helper function “get_field_session_value”). Since our “Sub Categories” and “Manufacturers” menus depend on values found within “Categories”, we don’t want to load them by default –- but instead, only when a “Categories” value is provided. However, if a “Categories” value was provided in a previous search, then we want that value to be open by default as the page is displayed. By replacing a special placeholder string, “<<field>>”, with any pre-specified value of the parent “Categories” menu, we are able to ensure that the appropriate list of OPTIONs is initially displayed.

This brings us to our next point. Some of our “populate_query” values contain the sub-string “<<field>>”. As you may have noticed, our helper function replaced that string with a dynamic value at run-time. The purpose of this is to allow our SELECT menus the option of being dependent upon each other using user-specified values. Our query, then, sets up all of the information necessary to allow for that interaction, except for the actual value of the key itself. That is provided at run-time as the user selects a “Categories” menu option.

Finally, our function removes any potential spaces from a field name before creating a corresponding HTML entity. This is done to prevent breakage in the way POST values are submitted for fields containing spaces in their names.

You may see the effects of our “get_search_field” function by inserting the following code into the “index.php” file. (This code should be inserted where the previous “<!-- … Menu here -->” placeholders were located.)

      <?php $search = new search(); ?>
      <td>
            <?php echo $search->get_search_field
( 'Categories' ); ?>
      </td>
      <td>
            <?php echo $search->get_search_field( 'Sub
Categories' ); ?>
      </td>
      <td>
            <?php echo $search->get_search_field
( 'Manufacturers' ); ?>
      </td>
      <td>
            <?php echo $search->get_search_field( 'Keywords' ); ?>
      </td>



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- 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: