I think I need a join but I'm not sure.

2 posts by 2 authors in: Forums > CMS Builder
Last Post: September 4, 2009   (RSS)

By InHouse - September 4, 2009

Situation:
Table 1 - List of event sponsors with num, title(text field), url(text field) and graphic upload.
Table 2 - List of events. Within this there is a checkbox list where the user can indicate which sponsors supported the event. The sponsor record number is being stored.

On the event detail page I'd like to show all the sponsors logos.

Some guidance on how best to do this would be appreciated. Not sure how best to walk the list of integers and pull the associated records all into one page.

J.

Re: [InHouse] I think I need a join but I'm not sure.

By Chris - September 4, 2009

Hi InHouse,

Here's how I did it. First, my STEP 1 code. You may need to change section and field names if ours aren't the same as mine:

list($eventRecords, $eventMetaData) = getRecords(array(
'tableName' => 'event',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',
));
$eventRecord = @$eventRecords[0]; // get first record

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

// fetch sponsors
$sponsorNums = join(", ", split("\t", trim($eventRecord['sponsors'])));
if ( $sponsorNums ) {
list($sponsorRecords, $sponsorMetaData) = getRecords(array(
'tableName' => 'sponsor',
'where' => "num IN ($sponsorNums)",
));
}
else {
$sponsorRecords = array();
}


And then, later in the page, when you want to display your sponsor images:

<?php foreach ($sponsorRecords as $sponsorRecord): ?>
<?php foreach ($sponsorRecord['graphic'] as $upload): ?>
<?php if ($upload['hasThumbnail']): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt='' /><br/>

<?php elseif ($upload['isImage']): ?>
<img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt='' /><br/>

<?php else: ?>
<a href="<?php echo $upload['urlPath'] ?>">Download <?php echo $upload['filename'] ?></a><br/>

<?php endif ?>
<?php endforeach ?>
<?php endforeach; ?>


I hope this helps! Please let me know if you have any trouble getting your page working, or if you have any questions.
All the best,
Chris