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 newsletter!

Error: Please check your email address.
Rocket to Success - Your 10 Tips for Smarter ERP System Selection
Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World
Show Comments

Cool Tech

SanDisk MicroSDXC™ for Nintendo® Switch™

Learn more >

Breitling Superocean Heritage Chronographe 44

Learn more >

Toys for Boys

Family Friendly

Panasonic 4K UHD Blu-Ray Player and Full HD Recorder with Netflix - UBT1GL-K

Learn more >

Stocking Stuffer

Razer DeathAdder Expert Ergonomic Gaming Mouse

Learn more >

Christmas Gift Guide

Click for more ›

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

Walid Mikhael

Brother QL-820NWB Professional Label Printer

It’s easy to set up, it’s compact and quiet when printing and to top if off, the print quality is excellent. This is hands down the best printer I’ve used for printing labels.

Ben Ramsden

Sharp PN-40TC1 Huddle Board

Brainstorming, innovation, problem solving, and negotiation have all become much more productive and valuable if people can easily collaborate in real time with minimal friction.

Sarah Ieroianni

Brother QL-820NWB Professional Label Printer

The print quality also does not disappoint, it’s clear, bold, doesn’t smudge and the text is perfectly sized.

Ratchada Dunn

Sharp PN-40TC1 Huddle Board

The Huddle Board’s built in program; Sharp Touch Viewing software allows us to easily manipulate and edit our documents (jpegs and PDFs) all at the same time on the dashboard.

George Khoury

Sharp PN-40TC1 Huddle Board

The biggest perks for me would be that it comes with easy to use and comprehensive programs that make the collaboration process a whole lot more intuitive and organic

David Coyle

Brother PocketJet PJ-773 A4 Portable Thermal Printer

I rate the printer as a 5 out of 5 stars as it has been able to fit seamlessly into my busy and mobile lifestyle.

Featured Content

Product Launch Showcase

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?