Sort Uploads by createdTime

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

I have a page that displays a gallery of photos that are grouped by a category. I need to sort the photos in each category by the createdTime system field in the uploads table so that the most recently uploaded photo appears at the top of the table inside each category header.

I have attached the page that I'm working on. I'm sure this is a rather simple task to do, but I'm missing something here.

I've tried to get the records from the uploads table and sort them as needed, but I cannot seem to display on the page. They need to be displayed inside the category foreach loop.

Any help will be appreciated.
Attachments:

gallery-new.php 5K

Re: [pcolvin] Sort Uploads by createdTime

By Jason - February 20, 2012

Hi,

You have the right idea. What's happening is that you're querying the uploads table to retrieve records for only the first gallery record, not all of them. What we can do is stop the getRecords call from getting uploads (no point doing it twice), and then getting upload records for each gallery record inside the foreach loop.

For example, start by turning off uploads for gallery records:

// load records
list($galleryRecords, $galleryMetaData) = getRecords(array(
'tableName' => 'gallery',
'loadUploads' => false,
));


Next, in your foreach loop, query the uploads table for the uploads associated with the current record:

<?php foreach ($galleryRecords as $record): ?>

<h2><?php echo $record['category'] ?><br/></h2>

<table border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
<tr>
<?php
//get uploads for current record
$uploads = mysql_select('uploads', "tableName='gallery' AND fieldName = 'photos' AND recordNum='".intval($record['num'])."' ORDER BY createdTime DESC");
?>
<?php foreach ($uploads as $upload): ?>

<td>

<?php if ($upload['thumbUrlPath']): ?>
<a href="<?php echo $upload['urlPath'] ?>" rel="lightbox[<?php echo $record['num'] ?>]" title="Description: <?php echo $upload['info1'] ?>">
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="" />
</a>

<?php else: ?>
<img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt="" />

<?php endif ?>

</td>

<?php $maxCols=4; if (@++$count % $maxCols == 0): ?></tr><tr><?php endif; ?>
<?php endforeach ?>

</tr>
</table>
<?php $count=0 ?>
<hr/>
<?php endforeach ?>


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/