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

Displaying Search Results - 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



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:

      $r_html                       = NULL;
      $r_row_count      = 0;
      # using helper function, run query and retrieve raw results
      $db_results = $search->get_search_results();     

      # if results have not been returned, display error message
      if ( !is_array( $db_results ) || count( $db_results ) ==
0 ) {
            $r_html     .= 'No results were found for the search
criteria you specified. Please try again.';     

      # if results have been found, output them properly
      } else {
            $r_html     .= 'Your search returned the following
results: <br><br>';
            $r_html     .= '<table width="100%">';
            $r_html     .= '<tr>';
            $r_html     .= '<th>' . $search->get_header_html( 'Components', 'pc_categories.name' ) . '</th>';
            $r_html     .= '<th>' . $search->get_header_html( 'Type', 'pc_sub_categories.name' ) . '</th>';
            $r_html     .= '<th>' . $search->get_header_html( 'Manufacturer', 'pc_manufacturers.name' ) . '</th>';
            $r_html     .= '<th>' . $search->get_header_html( 'Name',         'pc_parts.name' ) . '</th>';
            $r_html     .= '<th>Description</th>';
            $r_html     .= '</tr>';           

            # for each result found, return a table row
            foreach ( $db_results as $db_result ) {                 

                  # using row count, determine appropriate CSS
                  $l_css_class      = ( ++$r_row_count % 2 ==
1 ) ? 'odd' : 'even';                 

                  # output table row of results
                  $r_html     .= '<tr class="' . $l_css_class .
                  $r_html     .= '<td>' . $db_result
['category_name']         . '</td>';
                  $r_html     .= '<td>' . $db_result
['sub_category_name']     . '</td>';
                  $r_html     .= '<td>' . $db_result
['pc_manufacturer_name']  . '</td>';
                  $r_html     .= '<td>' . $db_result
['part_name']       . '</td>';
                  $r_html     .= '<td>' . $db_result
['part_description']      . '</td>';
                  $r_html     .= '</tr>';      
            } # END for each result           

            # close table
            $r_html     .= '</table>';

      } # END if results found
      # display results HTML
      echo $r_html;

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
      $f_css_class      = ( $this->get_order_by_field() ==
$p_sort_by_field ) ? 'class="selected"' : NULL;           

      # setup basic hyperlink
      $f_url      = $this->get_base_url() . '?' . $GLOBALS
['db_order_by_field_name'] . '=' . $p_sort_by_field;
      $r_html     = "<a href='$f_url' $f_css_class>$p_display_name</a>";           

      # return HTML tag
      return $r_html;           

} # END get_header_html()
function get_base_url() {     

      # retrieve current URL from browser
      $r_php_self = $_SERVER['PHP_SELF'];           

      # explode URL to retrieve only the current filename (w/ args)
      $r_php_self = explode( '/', $r_php_self );
      $r_php_self = end( $r_php_self );           

      # explode URL again to remove arguments, if any are present
      $r_php_self = explode( '?', $r_php_self );
      if ( is_array( $r_php_self ) ) $r_php_self = $r_php_self

      # return URL
      return $r_php_self;
} # END get_base_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 {
            color: #000000;
            text-decoration: underline;

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.

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