Sort order from pick-list

7 posts by 2 authors in: Forums > CMS Builder
Last Post: August 20, 2012   (RSS)

By Toledoh - August 16, 2012

Hi Guys,

I have a table of "downloads" and one field is a pick-list "category" made up of;

Brochure
Movie
Article
Case Study
Press Release

on the viewer, I'm sorting by category so I can group the list under that category heading, ie;

Brochure
- Item 1
- Item 4

Article
- Item 2
- Item 3
etc.

However, when sorting by category, the order of the categories in in alphabetical, (Article, Brochure, Case study...) rather than the way I have it listed in the drop down.

Can I fix this to sort as I want?

See the downloads on: http://www.fibreking.com/products.php?num=3

list($downloadsRecords, ) = getRecords(array(
'tableName' => 'downloads',
'where' => "products LIKE '%\t".intval($ProductItemsRecord['num'])."\t%'",
'allowSearch' => false,
));
$downloadsRecord = @$downloadsRecords[0]; // get first record


<div class="four columns sideColumn" id="downloads">
<!-- ALL CONTENT GOES WITHIN THIS DIV-->
<h2>Downloads</h2>
<img src="images/arrow.png" alt="arrow" width="18" height="17" />

<?php $currentCat = ''; ?>
<?php foreach ($downloadsRecords as $record): ?>
<?php $thisCat = $record['category']; ?>
<?php if ($thisCat != $currentCat): ?>
<?php $currentCat = $thisCat; ?>
<h4><?php echo $currentCat ?></h4>
<?php endif ?>
<p><?php foreach ($record['file'] as $index => $upload): ?>
<a href="<?php echo $upload['urlPath'] ?>" target="_blank" onClick="javascript: _gaq.push(['_trackPageview', '/downloads/<?php echo $record['title'] ?>']);"><?php echo $record['title'] ?></a>
<?php endforeach ?>
</p>
<?php endforeach ?>

<!-- ALL CONTENT GOES WITHIN THIS DIV-->
</div>

Cheers,

Tim (toledoh.com.au)

Re: [Toledoh] Sort order from pick-list

By Jason - August 16, 2012 - edited: August 17, 2012

Hi Tim,

One approach would be to organize your records by category using the array_groupBy() function. Then, you can retrieve an array of your list options from your schema and use this to output your categories in that order.

For example:

<?php $downloadsByCategory = array_groupBy($downloadsRecords, 'category', true); ?>
<?php foreach (getListOptions('downloads', 'category') as $value => $label): ?>
<h4><?php echo $label;?></h4>

<?php $downloads = @$downloadsByCategory[$value]; ?>
<?php if (!$downloads) { $downloads = array(); } ?>

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

<p>
<?php foreach ($record['file'] as $index => $upload): ?>
<a href="<?php echo $upload['urlPath'] ?>" target="_blank" onClick="javascript: _gaq.push(['_trackPageview', '/downloads/<?php echo $record['title'] ?>']);"><?php echo $record['title'] ?></a>
<?php endforeach ?>
</p>

<?php endforeach ?>

<?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/

Re: [Jason] Sort order from pick-list

By Toledoh - August 16, 2012 - edited: August 16, 2012

Thanks Jason,

But I'm getting an error with "<?php foreach ($record['file'] as $index => $upload): ?>"...

Warning: Invalid argument supplied for foreach() in /home/fibrekin/public_html/_downloads_grouped.php on line 13

Any thoughts?

BTW: The first <h4> coming up before the error is brochure... so it looks like it's going to work:)
Cheers,

Tim (toledoh.com.au)

Re: [Toledoh] Sort order from pick-list

By Jason - August 17, 2012 - edited: August 17, 2012

Hi Tim,

Ah, I see my mistake. $downloadsByCategory[$value] is an array of records, not an individual record. Try this change:


<?php $downloads = @$downloadsByCategory[$value]; ?>
<?php if (!$downloads) { $downloads = array(); } ?>

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


Also, make a change to this line here:

<?php $downloadsByCategory = array_groupBy($downloadsRecords, 'category', true); ?>


That should take care of the issue. I'll make the change in the above post as well.

thanks,
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

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

Re: [Jason] Sort order from pick-list

By Toledoh - August 17, 2012 - edited: August 17, 2012

That works great Jason!

However, now it lists every category, even if there's no downloads for that category... any way we can hide the

<h4><?php echo $label;?></h4>

if there's no $downloads for that category?

or... can I change <?php if (!$downloads) { $downloads = array(); } ?> to say "no <?php echo $label;?> records as yet"
Cheers,

Tim (toledoh.com.au)

Re: [Toledoh] Sort order from pick-list

By Jason - August 20, 2012

Hi Tim,

Try this:

<?php $downloadsByCategory = array_groupBy($downloadsRecords, 'category', true); ?>

<?php foreach (getListOptions('downloads', 'category') as $value => $label): ?>

<?php $downloads = @$downloadsByCategory[$value]; ?>
<?php if (!$downloads) { continue; } ?>

<h4><?php echo $label;?></h4>


This will skip over to the next category if there are no records for the current category.

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/