Let’s say you have a number of address book entries that contain things like name, address, phone, travel directions, map URL, etc. Or you have a number of products with detailed information about each of them.
Instead of retyping all of your information each time you want to display it on a list or detail page, you can pull that information directly from your master information list. Chris Waddell from Interactive Tools created a “relatedRecordLookupFunctions” plugin to add the required functionality this and suggested the following approach, I modified the table and field names for this “Address Book” example:
MULTI-VALUE LIST FIELDS (THE CODE REQUIRED FOR SINGLE VALUE LIST FIELDS FOLLOWS)
Step 1: Set up your address book section. for this example call it “Venue Address Book”.
This is a standard multi-record section with the necessary information fields. Since this example is an address book of venues for an events listing, I’ve set up fields for Venue Name, Venue Address, Venue Phone, Venue Contact E-mail, Venue URL, Venue URL link text (I like to keep these separate to add flexibility for my clients but I set the default text to WEB SITE or CLICK HERE FOR WEB SITE), Venue Travel Directions, and Venue Map URL (either from Mapquest or Google maps).
Step 2: In the section where you want this information to appear you’ll add a list type field called “Venue” with the following parameters.
Field Label: Venue Field Name: venue Field Type: list Field Options: Display As: pull-down or checkbox (multi-value for multi-value lists only) List Options: Get options from database (advanced) Section Table name: venue_address_book Use this field for option values: num Use this field for option labels: venue_name
Step 3: Install the Related Record Lookup Functions Plugin which you can download from:
http://www.thecmsbcookbook.com/downloads/relatedRecordLookupFunctions.zip
Upload the file to your server in the /cmsAdmin/plugins directory, then log into CMSB, go to Admin > Plugins, and click Activate on it.
LIST PAGES Step 3: To set up your viewer to display the fields required you'll need to add some code to the top of your page. You probably already have most of the required code in your PHP source code. Just add this code:
beta_lookupRelatedFields(array( 'table' => 'e_blast_events_notice', 'recordList' => &$e_blast_events_noticeRecords, 'fieldList' => array( 'venue' ) )); $e_blast_events_noticeRecord = @$e_blast_events_noticeRecords[0]; // get first record
to your existing code, like this:
<?php header('Content-type: text/html; charset=utf-8'); ?> <?php require_once "/your_path_to/cmsAdmin/lib/viewer_functions.php";
list($e_blast_events_noticeRecords, $e_blast_events_noticeMetaData) = getRecords(array( 'tableName' => 'e_blast_events_notice', 'where' => whereRecordNumberInUrl(1), 'limit' => '1', )); ?> <?php if ($e_blast_events_noticeRecords): ?> <?php beta_lookupRelatedFields(array( 'table' => 'e_blast_events_notice', 'recordList' => &$e_blast_events_noticeRecords, 'fieldList' => array( 'venue' ) )); $e_blast_events_noticeRecord = @$e_blast_events_noticeRecords[0]; // get first record
?> <?php endif ?>
<?php // show error message if no matching record is found if (!$e_blast_events_noticeRecord) { header("HTTP/1.0 404 Not Found"); print "Record not found!"; exit; }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
Step 4: Then in the body, where you want to display your address book fields:
<?php if (empty($e_blast_events_noticeRecord['venue'])): ?> No Venue Information is available. <?php else: ?> <div align="left"> Venue Information: <br />
<?php foreach ($e_blast_events_noticeRecord['venue'] as $venue): ?> <?php echo $venue['venue_name'] ?><br /> <?php echo $venue['venue_address'] ?><br /> <?php echo $venue['venue_contact_e_mail'] ?> <br /> <?php echo $venue['venue_phone'] ?><br /> Directions: <br /><?php echo $venue['venue_travel_directions'] ?> <br /> <a href="<?php echo $venue['venue_map_url'] ?>">Click / Tap for a Map</a><br /> <?php endforeach ?> </div> <?php endif ?>
You can display the fields separately but you’ll need a foreach loop for each field or set of fields that you want to display.
DETAIL PAGES:
To set up your viewer to display the fields required you'll need to add some code to the top of your page. You probably already have most of the required code in your PHP source code. Just add this code:
The <?php if ($e_blast_events_noticeRecords): ?> <?php beta_lookupRelatedFields(array( 'table' => 'e_blast_events_notice', 'recordList' => &$e_blast_events_noticeRecords, 'fieldList' => array( 'venue' ) )); $e_blast_events_noticeRecord = @$e_blast_events_noticeRecords[0]; // get first record ?> <?php endif ?>
Then, on the detail page where you want the information to be displayed:
<?php $venue = $e_blast_events_noticeRecord['venue']; ?> <?php echo $venue['venue_name'] ?><br /> <?php echo $venue['venue_address'] ?><br /> <?php echo $venue['venue_contact_e_mail'] ?> <br /> <?php echo $venue['venue_phone'] ?><br /> Directions: <br /><?php echo $venue['venue_travel_directions'] ?> <br /> <a href="<?php echo $venue['venue_map_url'] ?>">Click / Tap for a Map</a><br />
SINGLE VALUE LIST FIELDS According to Chris Waddell from Interactive Tools, “The process works with single value lists too, but the interface is slightly different.(Don't forget to make your list a single value pulldown or check box) The plugin replaces the field with the associated record, instead of a list of records. For this reason, you wouldn't use foreach to loop over the records.”
Here’s how. First, define the variable $venue before it's first use:
<?php $venue = $e_blast_events_noticeRecord['venue']; ?>
Then where you want to echo the fields in your viewer:
<?php echo $venue['venue_name']; ?>
And if you wanted to include an “if” statement the code would look like this:
<?php if ($venue['venue_name']): ?> <?php echo $venue['venue_name']; ?> <?php endif; ?>
Or you could leave out the define variable step and just use:
<?php echo $e_blast_events_noticeRecord['venue']['venue_name']; ?>
LIST PAGES If you want to implement this on a list page:
Again here’s the code for the head of the list page:
<?php require_once "/hsphere/local/home/apbcweb/artistsofpalmbeachcounty.org/cmsAdmin/lib/viewer_functions.php";
list($e_blast_events_noticeRecords, $e_blast_events_noticeMetaData) = getRecords(array( 'Table name' => 'e_blast_events_notice',
));
?> <?php if ($e_blast_events_noticeRecords): ?> <?php beta_lookupRelatedFields(array( 'table' => 'e_blast_events_notice', 'recordList' => &$e_blast_events_noticeRecords, 'fieldList' => array( 'venue' ) )); $e_blast_events_noticeRecord = @$e_blast_events_noticeRecords[0]; // get first record ?> <?php endif; ?>
And here’s the code to display the related records in the body of the list page:
<?php foreach ($e_blast_events_noticeRecords as $record): ?> <?php $venue = $record['venue']; ?> Location:<br /> <?php echo $venue['venue_name']; ?><br /> <span class="body-text"><?php echo $venue['venue_address'] ?><br /> <?php endforeach ?>
Note: Whether you’re using this once, or multiple times on your page, the:
<?php if ($e_blast_events_noticeRecords): ?>
before the:
<?php beta_lookupRelatedFields...
makes sure that if there are no records matching your particular criteria, you wont get a “lookupReferringRecords: 'recordList' option missing” error.
IMAGES When implementing this function for images, Dan Maitland from Heelatch.com found that the “if” statement was important.
<?php if ($venue['venue_image']): ?> <?php foreach ($venue['venue_image'] as $upload): ?> <img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="" /><br /> <?php endforeach ?> <?php endif; ?>
The code from the venue_address_book code generator was
<?php foreach ($venue_address_book['venue_image'] as $upload): ?>
I changed that to:
<?php foreach ($venue['venue_image'] as $upload): ?>
to match the rest of the code in my viewer.
Of course, as always, styling is up to you.
|