PHP Page 4 - Creating a Searchable Inventory System: Retrieving and Managing Search Results (continued) |
At this point, all of our basic search functions are in place and operating correctly – with one obvious exception. The search results are never displayed within our HTML file! Fixing that won’t be too hard though, because all we will need to do is retrieve the results (using our helper function, “get_search_results”) and then display them in whatever way we choose. As you may remember, “get_search_results” simply returns a group of associative arrays. The keys to these arrays are simply the fields we specified in our SELECT statement “db_base_query” earlier. Because we know these keys, we can easily display our results in an intelligent manner. For instance, one possible method of displaying the results is to list them in the following order: “Category”, “Sub Category”, “Manufacturer”, “Name”, and “Description”. This is the order we will be displaying results in, although it could easily be changed to another order if you choose to do so. Add the following PHP code in where the “<!-- Search Results here -->” placeholder is: <?php # if results have not been returned, display error message # if results have been found, output them properly # for each result found, return a table row # using row count, determine appropriate CSS # output table row of results # close table } # END if results found The above code is pretty straight forward. It retrieves a list of search results. If no results were found, an error message is displayed – otherwise, a table is constructed to contain all of the results returned. This is all pretty straightforward, which is good. The hard work should be done behind the scenes, by our helper class. In order to further streamline things, we also introduced an additional helper function in the above code, “get_header_html”. This function will handle displaying column headers, along with hyperlink tags, to allow users to easily sort the search results by clicking on a header. Let’s take a look at the internals of that function and also add it into our “search.php” page: function get_header_html( $p_display_name, $p_sort_by_field ) { # if field is currently selected, highlight it # setup basic hyperlink # return HTML tag } # END get_header_html() # retrieve current URL from browser # explode URL to retrieve only the current filename (w/ args) # explode URL again to remove arguments, if any are present # return URL As you can see, we actually introduced two new functions instead of one. The second, “get_base_url” is simple and merely returns the current URL, stripped of any arguments or directory names. This will be used not only by our “get_header_html” function but also our “get_pagination_menu” function that will be introduced in the next part of this series. The “get_header_html” function is fairly straightforward. It simply creates a hyperlink tag to allow the user to sort results by the given field, and then it checks to see whether records are currently sorted by the field in question. If so, it specifies the anchor tag as belonging to the CSS class “selected”. How you implement the “selected” class is up to you, and will vary widely depending on the surrounding HTML template. However, for our example application, I recommend you add the following CSS to the header of our “index.php” file: a.selected { In Conclusion We’ve now assembled all of the pieces of our application into a working search tool. If you’ve been following along, you will notice that you’re now able to search for records using any combination of the search form fields. You may also sort those records by any of the resulting column headers (except for “Description”) simply by clicking the column header once to sort ascending, and twice to sort descending. If you’ve not been following along you may still view a working version of this application here: http://portfolio.boynamedbri.com/devshed/search/part_4/ You may also download the source code for this application here: http://portfolio.boynamedbri.com/devshed/search/part_4/part_4.zip That’s it for this part of the tutorial, but in the next article we’ll take a look at creating a pagination menu to allow users to specify which page of results should be displayed. We’ll also be adding a couple of other, more advanced features to our search tool, so stick around.
blog comments powered by Disqus |
|
|
|
|
|
|
|