The goal is to create a list page using information from 2 multi record editors. “Exhibitions”, and “Opening Receptions”.
The “Exhibitions” editor has all of the permanent event information and the Opening Receptions” contains only the opening reception information for each exhibition. (I’m using 2 editors so that the “Opening Reception” information can automatically be hidden after the reception date has passed)
Here’s the code I thought would work, but it didn’t. Then you’ll see what did work, and understand why:
Since I was going to access 2 section editors, I put both my getRecords() calls In the head of the document:
<?php require_once "/yourpathto/cmsAdmin/lib/viewer_functions.php";
list($exhibitionsRecords, $exhibitions metadata) = getRecords(array( 'Table name' => 'exhibitions', )); list($opening_receptionsRecords, $opening_receptionsMetaData) = getRecords(array( 'Table name' => 'opening_receptions', 'where' => "exhibition LIKE '%" .mysql_real_escape_string(@$_REQUEST['sort_exhibition']). "%'", )); ?>
And then in the body:
<table> <?php foreach ($exhibitionsRecords as $record): ?> <tr> <td><a href="<?php echo $record['_link'] ?>">"<?php echo $record['exhibition'] ?>"</a> Show Dates: <?php echo date("D, M jS", strtotime($record['opening_date'])) ?> - <?php echo date("D, M jS, Y ", strtotime($record['closing_date'])) ?> <hr color="#C70000" width="300" />
Opening Reception: <?php foreach ($opening_receptionsRecords as $record): ?><?php echo date("D, M jS", strtotime($record['reception_date'])) ?><?php endforeach; ?> </td> </tr> <?php endforeach; ?> </table>
Using this approach, no matter how I adjusted the code, all of the “Opening Reception” dates were showing in each event listing.
I was out of ideas, when Dave suggested that my initial approach didn’t work because you can't look up all the opening dates without knowing all the exhibition titles in advance. So one way to do it is to load the opening date as you display each exhibition.
Instead of putting both getRecords() calls in the head of the document, set up one getRecords() in the document head that loads the exhibitions records. Then create the foreach loop to display them.
Then, inside that foreach loop, insert the other getRecords() call that loads the opening dates for each particular exhibition and a foreach loop displays those. Like this:
In the head:
<?php require_once "/yourpathto/cmsAdmin/lib/viewer_functions.php";
list($exhibitionsRecords, $exhibitions metadata) = getRecords(array( 'Table name' => 'exhibitions', ));
?>
Then in the Body:
<table> <?php foreach ($exhibitionsRecords as $record): ?> <tr> <td> <a href="<?php echo $record['_link'] ?>"> <class="style2">"<?php echo $record['exhibition'] ?>"</a> <br /> Show Dates: <?php echo date("D, M jS", strtotime($record['opening_date'])) ?> - <?php echo date("D, M jS, Y ", strtotime($record['closing_date'])) ?><br /> <?php echo $record['days_and_times'] ?></p><hr color="#C70000" width="300" />
Opening Reception: <?php list($opening_receptionsRecords, $opening_receptionsMetaData) = getRecords(array( 'Table name' => 'opening_receptions', 'where' => "exhibition = '" .mysql_real_escape_string(@$record['exhibition']). "'", )); ?>
<?php foreach ($opening_receptionsRecords as $reception): ?> <?php echo date("D, M jS", strtotime($reception['reception_date'])) ?> <?php endforeach; ?>
</td> </tr> <?php endforeach; ?> </table>
Dave’s approach worked like a charm, however, he pointed out that this adds MySQL queries, so if you’re displaying lots and lots of records this might not be a very efficient approach.
|