Related Files Sidebar

3 posts by 2 authors in: Forums > CMS Builder
Last Post: December 8, 2010   (RSS)

By zip222 - December 8, 2010

I am trying to do something and have been partially successful. I would like to use one multi-record section editor (related_files) to upload files so that they can then be selected in another multi-record section editor (pages) to appear on the sidebars of the pages. I have the two editors setup, and have used the mysql query list function in the 'pages' editor to allow the records in the 'related_files' editor to be selected. I also have the php page setup and have used the code below to grab the related record - but unfortunately I can't get it to grab and display more than one record.

list($relatedfilesRecord,$relatedfilesMetaDate)=getRecords(array(
'tableName' => 'related_files',
'allowSearch' => false,
'where' => "num = '".intval($page['related_files'])."'",
));

if($relatedfilesRecord){
$relatedfiles = $relatedfilesRecord[0]; //get first record
}
else{
$relatedfiles=array();
}


and then on the page, I am using this:

<ul>
<?php foreach ($relatedfiles['file'] as $upload): ?>
<li><a href="<?php echo $upload['urlPath'] ?>"><?php echo $relatedfiles['title']; ?></a></li>
<?php endforeach ?>
</ul>


i realize this code needs to have a foreach loop, but i have tried that and it didn't work. help?

Re: [zip222] Related Files Sidebar

By Chris - December 8, 2010

Hi zip222,

I'm guessing that your 'related_files' field is a multi-value list field? If that's the case, $page['related_files'] will contain a tab-separated list of nums. Here's how to work with that:

$commaSepNums = implode(", ", array_map('intval', explode("\t", trim($page['related_files']))));

$relatedfilesRecord = array();
if ($commaSepNums) {
list($relatedfilesRecord,)=getRecords(array(
'tableName' => 'related_files',
'allowSearch' => false,
'where' => "num IN ($commaSepNums)",
));
}


...and then in your page:

<ul>
<?php foreach ($relatedfilesRecord as $relatedfiles): ?>
<?php foreach ($relatedfiles['file'] as $upload): ?>
<li><a href="<?php echo $upload['urlPath'] ?>"><?php echo $relatedfiles['title']; ?></a></li>
<?php endforeach ?>
<?php endforeach ?>
</ul>


I hope this helps! Please let me know if you have any questions.
All the best,
Chris