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!

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

Computerworld
Show Comments

Cool Tech

Breitling Superocean Heritage Chronographe 44

Learn more >

SanDisk MicroSDXC™ for Nintendo® Switch™

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

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.

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

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?