Searching address book data in PHP

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.

Python vs. PHP: Choosing your next project's language

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.

Join the PC World newsletter!

Error: Please check your email address.

Struggling for Christmas presents this year? Check out our Christmas Gift Guide for some top tech suggestions and more.

Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World

Most Popular Reviews

Follow Us

Best Deals on GoodGearGuide

Shopping.com

Latest News Articles

Resources

GGG Evaluation Team

Kathy Cassidy

STYLISTIC Q702

First impression on unpacking the Q702 test unit was the solid feel and clean, minimalist styling.

Anthony Grifoni

STYLISTIC Q572

For work use, Microsoft Word and Excel programs pre-installed on the device are adequate for preparing short documents.

Steph Mundell

LIFEBOOK UH574

The Fujitsu LifeBook UH574 allowed for great mobility without being obnoxiously heavy or clunky. Its twelve hours of battery life did not disappoint.

Andrew Mitsi

STYLISTIC Q702

The screen was particularly good. It is bright and visible from most angles, however heat is an issue, particularly around the Windows button on the front, and on the back where the battery housing is located.

Simon Harriott

STYLISTIC Q702

My first impression after unboxing the Q702 is that it is a nice looking unit. Styling is somewhat minimalist but very effective. The tablet part, once detached, has a nice weight, and no buttons or switches are located in awkward or intrusive positions.

Latest Jobs

Shopping.com

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?