HRDIODB
[ class tree: HRDIODB ] [ index: HRDIODB ] [ all elements ]
Prev Next
Writing New Predefined Searches

Writing New Predefined Searches

This tutorial will walk you through the process of adding a predefined search to the "Predefined Searches" menu. Predefined searches may offer unique searches, unavailable through other interfaces, or be simplifications of more complicated searches. They can also make use of the free form search functions to provide a static interface for an otherwise complex set of free form search entries. For this tutorial we will write a simple search that finds all UARS Days with a given pid number.

Written by Ziba R. Scott and Adam D. Gorski

Table of Contents

The Three Searching Components

Every search in HRDIODB has its code split into three components and each component is in a separate file.

  • search_forms.php - Display elements for both data entry and results
  • search.php - Flow control logic governing the execution of a search
  • search_class.php - All logic which composes/executes a SQL query and returns the values

search_forms.php - Draw the form

In search_forms.php we need to add code in two places: One to add your search to the menu and one to draw the search entry form.

Add Your Search to the Menu

The predefined_search_forms() function governs the display of predefined searches in the navigation menu. Add your new search to the array of search names and add a call to a function to draw your form in the switch statement below. If you need to populate any variables to pass to your drawing function, do that now.

  1. $searches[0]="Pid, Date, Latitude Search";
  2. // My new search name
  3. $searches[1]="Pid Search";
  4. .
  5. .
  6. .
  7. // Variables needed by other search functions
  8. $pids $hdb->get_table("HRDI_Pids");
  9. $types $hdb->get_table("HRDI_FileTypes");
  10. .
  11. .
  12. .
  13. // Place all search form printing functions here
  14. switch($search_num){
  15.     case 0:
  16.       pid_day_coverage_form($got$pids$types);
  17.       break;
  18.     case 1:
  19.       // My new search form drawing function 
  20.             pid_form($got$types);
  21.       break;
  22. }
Write Your Search Drawing Function

Write a function that will draw your search form. Check out common_class.php for convenience functions you may wish to use. Also note that you can pass the $got variable to you function which is a copy of the $_GET variable that has been checked for security risks. You may also include the file_filter_form() function which will include a table of check boxes and file types. If used, it will allow users to find the intersection of your search results and the selected file sets. This must be included within your form tags.

  1. function pid_form($got, $types){
  2. ?>
  3.   <!-- Tables class are used for a consistent look -->
  4.   <table class="hrdi">
  5.     <tr class="c_label">
  6.      <td>
  7.      Instructions
  8.      </td>
  9.     </tr>
  10.     <tr>
  11.       <td>
  12.       Enter a pid and press search.
  13.       </td>
  14.     </tr>
  15.   </table>
  16.  
  17.   <!-- Using the "get" method allows users to bookmark
  18.        searches, but also invites tinkering.
  19.        Please use the $got variable to access submitted
  20.        data for safety.  -->
  21.   <form name="search" method="get">
  22.     <table class="hrdi">
  23.       <tr class="c_label">
  24.         <td>
  25.          Pid Search
  26.         </td>
  27.       </tr>
  28.       <tr>
  29.         <td>
  30.         <!-- This tells search.php to run a search and 
  31.              which search to run -->
  32.         <input type="hidden" name="submitted" value="pid">
  33.         <!-- This tells search_form.php to return to this 
  34.              view after it is submitted -->
  35.         <input type=hidden name=predefined_search value=1>
  36.         <!-- This is the data we are submitting.  The
  37.              previous value (if any) is repopulated.
  38.              The @ sign suppresses the warning generated
  39.              by php if "pid" is not a member of array "got". -->
  40.         <input type=text name=pid value=<?php print(@$got['pid'])?>>
  41.         <input type=submit name="" value="Search">
  42.         </td>
  43.       </tr>
  44.     </table>
  45.     <?php file_filter_form($types$got)?>
  46.   </form>
  47. <?php
  48. }

search.php - Call the desired search function

To connect your search form to piece of search logic you will need to add code to the flow control contained in search.php. For this tutorial we will be using the flexible search::free_form() function because it meets our needs. Near the end of search.php, not within a function call are the search trigger statements which check the "submitted" variable. Add one that matches the value of your "submitted" variable which will execute the code you desire.

  1. // Process a predefined search entry (Sample of an existing search trigger section)
  2.     if($got['submitted']=="pid_day_coverage"){
  3.     $status =  $search->pid_day_coverage($got['pid']
  4.                                         @$got['date_type'],
  5.                                          $got['date_value_low'],
  6.                                          $got['date_value_high']
  7.                                          $got['lat_day_min']
  8.                                          $got['lat_day_max']
  9.                                          $got['lat_night_min']
  10.                                          $got['lat_night_max']);
  11.   }
  12.   //New code which will be activated on form submit
  13.     if($got['submitted']=="pid"){
  14.     // Compose conditions for the free_form() search based on submitted information
  15.     // in the $_GET variable
  16.         $conditions[0]["Table"]="HRDI_Modes";
  17.     $conditions[0]["Field"]="Process_ID";
  18.     $conditions[0]["Operator"]="=";
  19.     $conditions[0]["Value"]=@$got['pid'];
  20.  
  21.     // Call the free_form() method of the search class
  22.         $status =  $search->free_form($conditions);
  23.   }
  24. }

And you're done! The search::free_form() function is very flexible and highly reusable. However, if you wish to implement custom search logic, read on to the next section.


search_class.php - Search Logic

Adding new search methods to the search class requires that the results be stored as an array of UARS Days in $this->results. This is where the result display and file type filtering functions expect to find them. It is also highly recommended that you make use of the hrdi_db class contained in the search object for convenience and security. If possible use $this->hdb->get_column() to get a simple array of values. The method should return FALSE if the query has failed and an empty array if there are no results (which get_column() will do).

Here is a simple example of a search method that would implement all the logic necessary to operate the search implemented previously in this tutorial. Note that it is an unnecesary method because the search::free_form() function can be made to perform the same query.

  1. function pid_search($pid){
  2.   $query "SELECT UARS_Day from HRDI_Modes WHERE Process_ID='$pid'";
  3.   return $this->results $this->hdb->get_column($query);
  4. }

Prev   Next
Reusing the uars_day Class HRDI Observational Database Documentation

Documentation generated on Tue, 23 Jan 2007 22:57:41 -0500 by phpDocumentor 1.3.0RC6