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!

Or

Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.
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

Toys for Boys

Family Friendly

Stocking Stuffer

SmartLens - Clip on Phone Camera Lens Set of 3

Learn more >

Christmas Gift Guide

Click for more ›

Brand Post

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

Aysha Strobbe

Microsoft Office 365/HP Spectre x360

Microsoft Office continues to make a student’s life that little bit easier by offering reliable, easy to use, time-saving functionality, while continuing to develop new features that further enhance what is already a formidable collection of applications

Michael Hargreaves

Microsoft Office 365/Dell XPS 15 2-in-1

I’d recommend a Dell XPS 15 2-in-1 and the new Windows 10 to anyone who needs to get serious work done (before you kick back on your couch with your favourite Netflix show.)

Maryellen Rose George

Brother PT-P750W

It’s useful for office tasks as well as pragmatic labelling of equipment and storage – just don’t get too excited and label everything in sight!

Cathy Giles

Brother MFC-L8900CDW

The Brother MFC-L8900CDW is an absolute stand out. I struggle to fault it.

Luke Hill

MSI GT75 TITAN

I need power and lots of it. As a Front End Web developer anything less just won’t cut it which is why the MSI GT75 is an outstanding laptop for me. It’s a sleek and futuristic looking, high quality, beast that has a touch of sci-fi flare about it.

Emily Tyson

MSI GE63 Raider

If you’re looking to invest in your next work horse laptop for work or home use, you can’t go wrong with the MSI GE63.

Featured Content

Product Launch Showcase

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?