Detail viewer / related table

3 posts by 2 authors in: Forums > CMS Builder
Last Post: April 27, 2012   (RSS)

By Toledoh - April 26, 2012

Hi Guys,

I still can't get my head around related items... can you help?

I've a table: "our_beers" that has a multitude of beer with fields title, num, description, image etc

I have created another single record editor "manly_taps" where I've created a number of fields tap_1, tap_2 etc that or pull down lists from "our_beers" with value:num & label:title.

Then, I can easily list the beers of tap at manly via;


// load records
list($manly_tapsRecords, $manly_tapsMetaData) = getRecords(array(
'tableName' => 'manly_taps',
'allowSearch' => '0',
'limit' => '1',
));
$manly_tapsRecord = @$manly_tapsRecords[0]; // get first record

// show error message if no matching record is found
if (!$manly_tapsRecord) { dieWith404("Record not found!"); }

and
Tap 1: <?php echo $manly_tapsRecord['tap_1:label'] ?>

What do I need to do to add more information to tap_1... such as the description, images etc.

Thanks!
Cheers,

Tim (toledoh.com.au)

Re: [Toledoh] Detail viewer / related table

By Jason - April 27, 2012

Hi Tim,

The easiest way to do this would be to first get an array of all your beer records, where the index of your array is the record num of the beer record.

You can do that like this:

<?php

list($beerRecords, ) = getRecords(array(
'tableName' => 'our_beers',
'allowSearch' => false,
));


$beerNumToRecord = array_groupBy($beerRecords, 'num');

?>


Next, you can set up a foreach loop to output each one of your taps, 1 at a time. In this example code, we assume that you are using 4 taps (tap_1 - tap_4). This can be adjusted by changing the value of $numberOfTaps

EXAMPLE:



<?php $numberOfTaps = 4; ?>

<?php foreach (range(1, $numberOfTaps) as $tapNum): ?>
<?php $tapField = "tap_$tapNum"; ?>

<?php if (!@$manly_tapsRecord[$tapField]) { continue; } ?>

Tap <?php echo $tapField;?> : <?php echo $manly_tapsRecord["$tapField:label"]; ?>

<?php
//get related beer record
$beer = $beerNumToRecord[$manly_tapsRecord[$tapField]];
?>

<?php
//ouput beer information here
?>

<?php endforeach ?>


So in this foreach loop, $beer is the our_beer record that was selected for the tap that is currently being output.

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/