Tuesday | 2 December, 2008
Searching address book data in PHP
Gavin Sherry (PC World) 01/01/2002 10:16:20

We have looked at storing address book entries submitted by users. The other requirement of an address book is to be able to locate an address. Now we will look at parsing address book entries and matching a user-supplied string: a search engine.

The submission side of AB1 allowed the storage of the following data: the person's first and last name; their street number, street name, suburb, post code and state; their telephone area code and land line phone number; and, optionally, their mobile phone number and e-mail address. Each of these items was stored on its own line with one or two leading prefixes attached so that the parser could identify the nature of the data on any given line. The search system will now use this structure.

Implementing A Search System


A powerful address book would allow the user to search for keywords in all the useful fields in the address book: first name, last name, street name and so on. In order to illustrate how this would be done, the following script implements a search on last name. Since it is a long script, some of the code has been omitted.

<? 
/* search for the last name stored in $query */if(isset($submit) &&
 (strcmp($submit,"Search") == 0)) { 
  $fp = fopen("address.dat","r"); 
   while(!feof($fp)) { 
    $line = fgets($fp,1024); 
     switch($line[0]) { 
      case '-': 
       /* end of item: was it a match? */ 
        if($match) { 
         $match = 0; // reset 
           /* out put the data */ 
. . . 
} 
break; 
case 'F': 
  /* save the first name, in case last name 
    * is a match */ 
     $fn = $line; 
     break; 
case 'L': 
if(strcmp(substr($line,1,strlen($line) - 2,$query) == 0) { 
$match = 1; // last name found 
$ln = substr($line,1); 
   } else { 
continue; // no match 
  } 
  break; 
case 'A': 
if($match) { 
if($line[1] == '1') $a1 = substr($line,1); 
if($line[1] == '2') $a2 = substr($line,1); 
. . . 
} 
  } 
  fclose($fp); 
} 

?>

&sp;&sp;When the user clicks the 'Search' button to submit the HTML form, the variable $submit is set to 'Search'. Hence, by testing if $submit is set and if it is set to 'Search', the script can hand control to the search code at the correct time, i.e., when a search has been required.

The script opens the data file and cycles through it, parsing each line to see what the data is. Each line is saved in case it is required to be output (that is, if a match is made). See the code (above) for some sample data.

Notice that it is the third case in the switch control structure that does the real work: namely, to match the search query against the last name in the file. If this succeeds, the script registers that a match is made; otherwise, the script advances to the next line of the file.

When the next line is tested by the switch (in this case, a line beginning with 'A'), the situation is more complicated. There are five lines beginning with A: A1, A2, A3, A4 and A5. As such, the script checks the second character to verify which line it is. Once this is achieved, the script tests to see if we need to save data given a last name match: if yes, the save is made; otherwise, the script continues to loop.

When the script encounters a dash ('-'), it checks to see if a match has been found. If so, the script outputs the address book data and resets the $match variable to false.

Anyone interested in extending their PHP skills should attempt to modify this code to handle on last name and first name (or any other field, for that matter).

Address Book 1 is fairly limited: searching for data can only occur on last names and searching is linear (that is, the entire file is searched from top to bottom). This is a very inefficient way to search data. In order to illustrate this, I tested AB1 with large amounts of data. When there were 200,000 addresses, searching took half a second; when there were 400,000 addresses, searching took 1 second; but, when there were 800,000 addresses, searching took over 3 seconds. Though AB1 has not been designed with such large amounts of data in mind, this highlights how poorly linear text searches perform.

Additional Resources
Executive Guides
Whitepapers
Zones
Zone logoZones provide focussed content from Computerworld and leading technology partners.
Newsletter Subscription
Sign up for our Computerworld newsletters!
RSS Feeds
Market Place

 

Smart SOA World Tour

Discover how SOA can create smarter outcomes for your business.

Attend and learn:

  • How SOA is helping leading companies to become more agile
  • Where you should be applying SOA processes in your company
  • The top SOA implementation mistakes to avoid

Click here for more information.
Whitepaper

Wireless LANs: Is my enterprise at risk?

Achieve an overall understanding of the risks associated with wireless LANs. Discover their inherent properties, as well as what makes them different from wired networks. Read on to uncover a list of recently published articles on real-life breaches and incidents illustrating the need for proactive measures to mitigate wireless security risks.

Enterprise IT Buyer's Guide
Find Technology Vendors Fast
 
Find vendors by name | Find by category
Sponsored Links