HomePHP Page 2 - Creating a Searchable Inventory System: Setting Up Your Database and User Interface (continued)
Helper Search Class - 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.
To start things off, our search.php file should contain the following:
<?php class search {
function get_search_field( $p_search_field_name ) { $r_html = NULL;
# return HTML return $r_html;
} # END get_search_field()
} # END class search ?>
The next step is to think about the information this function will need in order to operate properly. First, it will need to know the type of form field we wish to create (SELECT menu or text input). If we wish to create a SELECT menu, it will also require some additional information, such as the SQL query needed to retrieve a list of SELECT options. Since we are creating several of these select menus to depend on another menu, we will also need to tell the “get_search_field” function whether a field is dependent upon itself, or another field.
This can all be done through the use of associative arrays. Furthermore, our “inc.conf.php” file is a great place to do all of this, so let’s go ahead and set up the arrays by adding the following code:
# configure and append categories array $categories_array = array(); $categories_array['field_type'] = 'select'; $categories_array['populate_query'] = 'SELECT id AS value, name AS display FROM pc_categories ORDER BY name ASC;'; $categories_array['parent_menu'] = NULL; $categories_array['html_attributes'] = 'onChange="attach_file( \'helper.php?category_id=\' + this.options[ this.selectedIndex ].value );"';
# configure and append sub categories array $sub_categories_array = array(); $sub_categories_array['field_type'] = 'select'; $sub_categories_array['populate_query'] = 'SELECT id AS value, name AS display FROM pc_sub_categories WHERE category_id = <<field>> ORDER BY name ASC;'; $sub_categories_array['parent_menu'] = 'Categories'; $sub_categories_array['html_attributes'] = NULL;
# configure and append manufacturers array $manufacturers_array = array(); $manufacturers_array['field_type'] = 'select'; $manufacturers_array['populate_query'] = 'SELECT id AS value, name AS display FROM pc_manufacturers WHERE category_id = <<field>> ORDER BY name ASC;'; $manufacturers_array['parent_menu'] = 'Categories'; $manufacturers_array['html_attributes'] = NULL;
# add each field to main search array $search_array['Categories'] = $categories_array; $search_array['Sub Categories'] = $sub_categories_array; $search_array['Manufacturers'] = $manufacturers_array; $search_array['Keywords'] = $keywords_array;
# set search array to be global $GLOBALS['search_array'] = $search_array;
As you can see, we have created a GLOBAL array, “search_array”, and added within that array several other arrays –- each describing a particular field of our search form. Each field for instance specifies a “file_type”, either “select” or “text”. If the field is of type “select” then the array also specifies the query that will be required to retrieve a list of all possible SELECT values, and so forth. We’ll take a closer look at each of these elements as we create our helper Search class, which we are now ready to start on.