Re: [InHouse] Ordering by Multiple Value Lists
Hi J,
Here is some code you can try. Note, this example assumes that your groups are stored in a separate table and that the value of the list field in your main table is num. If this is not the case, let me know and we can modify this code slightly.
Also, this example is using a table called "jobs" with a field called "category", but you can change these names to match what you're using:
First we select our records, create an array that holds our category numbers and names, and then create a Array that holds all of our records, sorted into different groups:
<?php
list($jobRecords,$jobMetaData)=getRecords(array(
'tableName' => 'jobs',
'orderBy' => "title",
));
list($categoryRecords,$categoryMetaData)=getRecords(array(
'tableName' => 'new_category',
));
$categoryNumToName=array();
foreach($categoryRecords as $category){
$categoryNumToName[$category['num']] = $category['title'];
}
$orderedGroups=array();
foreach($jobRecords as $job){
$groups = trim($job['category'],"\t");
if($groups){ //record has groups
$groups = explode("\t",$groups);
foreach($groups as $group){
$orderedGroups[$group][]=$job;
}
}
else{ //record has no group
$orderedGroups['No Group'][]=$job;
}
}
?>
Finally, we output the contents of $orderedGroups:
<?php foreach ($orderedGroups as $groupTitle => $group): ?>
<span style="font-weight:bold;">
<?php if(is_numeric($groupTitle)): ?>
<?php echo $categoryNumToName[$groupTitle]; ?>
<?php else: ?>
<?php echo $groupTitle; ?>
<?php endif ?>
</span><br />
<ul>
<?php foreach($group as $item): ?>
<li><?php echo $item['title'];?></li>
<?php endforeach ?>
</ul></br>
<?php endforeach ?>
Give this a try and let me know if you run into any problems. If you do have problems, please attach the .php file you're working with so that I can take a closer look.
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/