Covisint upgrades catalog-management system

This month, we will look at modifying last month's AB2 code to allow searching of multiple data fields.

WORKING WITH INDICES

AB2 is designed to store address data in relation to the last name of the person's address. In database talk, AB2 is 'building an index' over the last names in the data received, since the last name is used as the 'key' to the database. To enhance the functionality of AB2, we will also build an index over the first name. This is referred to as 'building a secondary index' on the data set. Together, the two are referred to as indices.

There are several ways we can do this:

1) Build two databases, the first with the last name as a key, the second with the first name as the key.

2) Assign each new entry a unique identifier. Build two databases, the first relating last names and first names to identifiers by storing the last name and first name as the key and the identifier as the value, and the second relating identifiers to address book data.

Of these two, the first option is the simplest to implement so we will take this as a starting point. Those wanting to hone their PHP and database skills should attempt to implement the second.

Here, then, is an implementation of the data submission system for the first option:

$DBLN="/tmp/address-ln.db";

$DBFN="/tmp/address-fn.db";

if(isset($submit) && (strcmp($submit,"Add") == 0)) { $lnid = mkdb($DBLN); $fnid = mkdb($DBFN); $data = "F=".rawurlencode($fn)."&L=".rawurlencode($ln)."&" /* etc... */ if(dba_insert($ln,$data,$lnid)) { echo "Data successfully stored in $DBLN
"; dba_close($lnid); } else { echo "Could not store data in $DBLN!"; dba_close($lnid); exit(); } if(dba_insert($fn,$data,$fnid)) { echo "Data successfully stored in $DBFN
"; dba_close($fnid); } else { echo "Could not store data in $DBFN!"; dba_close($fnid); exit(); } } ?>This code is quite straightforward. Instead of submitting data to the database '/tmp/address.db' as AB2 did, AB2.1 inserts the data into two databases: '/tmp/address-fn.db' and '/tmp/address-ln.db', which builds an index across the last name.

Now for the search:

$DBLN="/tmp/address-ln.db";

$DBFN="/tmp/address-fn.db";

if(isset($submit) && (strcmp($submit,"Search") == 0)) { if(!file_exists($DBFN) || !file_exists($DBLN)) { /* database has not been created yet */ exit("No entries to search"); } /* search by ln first */ if(!($lnid = dba_open($DBLN,"r","db3"))) { exit("Could not open $db\n"); } if(($str = dba_fetch($query,$lnid))) { /* found the query */ parse_str($str); ?> Name: echo rawurldecode($F); ?> echo rawurldecode($L); ?>

/* etc... */

$lnhash = md5($str);

}

dba_close($lnid);

if(!($fnid = dba_open($DBFN,"r","db3"))) { exit("Could not open $DBFN\n"); } if(($str = dba_fetch($query,$fnid))) { /* found the query,same as the last? */ if(strcmp(md5($str),$lnhash) != 0) { parse_str($str); ?> Name: echo rawurldecode($F); ?> echo rawurldecode($L); ?>

/* etc... */

}

}

dba_close($fnid);

}

?>

This code is a little trickier. Like AB2, the search script here first attempts to search for the last name, this time in the database '/tmp/address-ln.db'. The script then attempts to search for $query as a first name, by querying '/tmp/address-fn.db'. Note that in the second search, the script attempts to avoid duplicate output (which could occur if a submit to the address book had identical first and last name fields). It does this with the help of the message-digest algorithm, MD5, which builds a unique representation of any given length of text in the form of a 16-character string. (More information can be found at www.faqs.org/rfcs/rfc1321.html.)By building MD5 message-digests of the data retrieved from a search of each database, the script can determine very easily whether or not the data output for the last name search is identical to the data retrieved in the first name search. If the message-digests are not equal, any results from the second search are output.

SHORTCOMINGS OF AB2.1

The implementation of secondary indices in AB2.1 still requires that more code be written for every field, which needs to be indexed. In all fairness, this is a flaw in PHP's Database Abstraction layer, which does not allow the user access to Berkeley DB's native support of secondary indices.

There are other problems, too: the Database Abstraction layer does not properly support multiple database users at any given time. In the world of databases, this functionality is called 'Multi-version Concurrency Control'. Next month, we will begin our first foray into the world of Relation Databases by focusing on PHP and PostgreSQL.

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

Computerworld
Show Comments

Brand Post

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

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.

Laura Johnston

MSI GS65 Stealth Thin

If you can afford the price tag, it is well worth the money. It out performs any other laptop I have tried for gaming, and the transportable design and incredible display also make it ideal for work.

Andrew Teoh

Brother MFC-L9570CDW Multifunction Printer

Touch screen visibility and operation was great and easy to navigate. Each menu and sub-menu was in an understandable order and category

Louise Coady

Brother MFC-L9570CDW Multifunction Printer

The printer was convenient, produced clear and vibrant images and was very easy to use

Edwina Hargreaves

WD My Cloud Home

I would recommend this device for families and small businesses who want one safe place to store all their important digital content and a way to easily share it with friends, family, business partners, or customers.

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.

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?