PHP Page 4 - Creating a Searchable Inventory System: Setting Up Your Database and User Interface (continued) |
Our search form is almost completed, with one exception: the dynamic SELECT menus do not yet work. As you may have noticed, our “Categories” menu specified an additional HTML attribute. This attribute called our JavaScript function, “attach_file”, whenever its onChange event was triggered. onChange="attach_file( 'helper.php?category_id=' + Another way of stating the above code would be to say that each time a “Categories” option is selected, the “helper.php” file is called and as a parameter, it receives the ID value of the category selected. In order to complete our search form then, we will need to complete the “helper.php” file by providing the following code: <?php # include all additional files # retrieve Category ID from URL # update child select menus # helper function to automate child menu update ?> // erase all current (child) SELECT menu options // add empty "Any" option to menu # if a category has been specified # run SQL query to return all child menu options # outout a SELECT menu option for each row # show all retrieved (child) SELECT # add new option to child menu } # END while results } # END update_child_select() As you may have noticed, the above code is a mix of PHP and JavaScript. This is because our PHP file is being attached to the document as a JavaScript file. It is therefore capable of executing not only PHP code but JavaScript as well. This is a very important concept, as it will allow us to not only retrieve a list of appropriate values from our SQL database, but also dynamically update the search form’s SELECT menus with those values. All our file does, in a nutshell, is retrieve the Category ID from the URL, run a query to retrieve all “Sub Category” and “Manufacturer” records found belonging to that ID, and then update the SELECT menus to contain those new values. This is made a little cleaner through the use of our helper function, “update_child_select”, but should still be relatively self-explanatory. Additional information on this technique may be found in my recent two-part article posted on Dev Articles entitled “PHP and JavaScript -- Pooling Resources”. Note: As before, we remove any spaces present in the field name before retrieving the HTML element object. In Summary At this point, our page layout has been set up using HTML and CSS, our code flow has been defined, and our helper Search class has been initiated. Our application now has all of the logic necessary to intelligently create a search form for users to input their search criteria. What’s more, this logic is all easily reusable and requires little more than updating the logic found within our “inc.conf.php” (and possibly the “helper.php”) files. This is a good start, as we have now laid the ground-work for part three of this article, which will cover retrieving a list of result records and sorting those records by each field specified. The real meat of the application lies there, but it is important to first establish an easily extensible interface with which users may conduct searches. So far we are right on track. If you would like to see a working example of this application, you may do so here: http://portfolio.boynamedbri.com/devshed/search/part_2/. If you would like to download source code for this application, you may access that here: http://images.devshed.com/ds/stories/Creating_Searchable_Inventory_System/
blog comments powered by Disqus |
|
|
|
|
|
|
|