Ordering by Multiple Value Lists
23 posts by 4 authors in: Forums > CMS Builder
Last Post: December 17, 2010 (RSS)
By InHouse - July 7, 2010 - edited: July 7, 2010
Looking for a bit of help in sorting a long list of records organized into sub-groups using a list. Here's the layout:
Table1 in CMSB has:
- title
- content
- groups : drop menu with list of options as types for that content.
- user can multi-select several options.
- an item will either have no group selected, or 1+ groups selected.
Sample Data:
Item 1 = GroupA, GroupB
Item 2 = GroupB
Item 3 = GroupA
Item 4 = no group selected
Need to display a list of these records which are sorted by their group designation. Within each group, the records should be sorted alphabetically by their title.
i.e.:
GroupA
- Item 1 title
- Item 3 title
GroupB
- Item 1 title
- Item 2 title
No Group
- Item 4 title
Just for extra fun, this page does use CMSB filtering based on the URL:
.../recipes_by_type.php?type=Main Courses
The "type" field is another level of sorting which does not figure into the immediate request above. But I suspect it will complicate things a bit down the line.
Given how the info for drop menu lists is stored I'm at a loss of how to do this.
Any help would be greatly appreciated.
Best wishes,
J.
Re: [InHouse] Ordering by Multiple Value Lists
By Jason - July 8, 2010
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/
Re: [Jason] Ordering by Multiple Value Lists
By InHouse - July 8, 2010
In our case, the group list is not a separate table, but rather just a list stored in the main table.
i.e.:
Table = recipes
Group = recipes.group as a checkboxes (multi value) list containing the elements:
Beef|Beef
Chicken|Chicken
Eggs|Eggs
Seafood|Fish/Seafood
Pasta|Pasta
Pork|Pork
Turkey|Turkey
Vegetarian|Vegetarian
I think that will affect the approach you suggest.
J.
Re: [InHouse] Ordering by Multiple Value Lists
By Jason - July 8, 2010
Yes, this does change it, but it actually makes it simpler. Here's the revised code still using the "jobs" example:
<?php
list($jobRecords,$jobMetaData)=getRecords(array(
'tableName' => 'jobs',
'orderBy' => "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;
}
}
?>
<?php foreach ($orderedGroups as $groupTitle => $group): ?>
<span style="font-weight:bold;"><?php echo $groupTitle; ?></span><br />
<ul>
<?php foreach($group as $item): ?>
<li><?php echo $item['title'];?></li>
<?php endforeach ?>
</ul></br>
<?php endforeach ?>
Give that a try and let me know if this works for you.
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] Ordering by Multiple Value Lists
By InHouse - July 8, 2010
Great solution and one which I'll be able to apply elsewhere also.
Many thanks!
J.
Re: [InHouse] Ordering by Multiple Value Lists
By studio-a - December 10, 2010 - edited: December 10, 2010
This thread has been very helpful. However, say if we have a field for the Group Description. This description field is located in the table 'new_category' - How do we include this within the arrays to work properly? It looks simple, yet we cannot get it to work.
For example the output would look something like this:
----------------------------------------------------------------------
Group Title 01 Placement Goes Here
Description: Text will be placed here and continue erat et tristique viverra, turpis purus tincidunt lacus, nec condimentum nisl neque sit amet augue. Ut tellus arcu, tempor ultrices.
- list item for group 01
- list item for group 01
- list item for group 01
- list item for group 01
Group Title 02 Placement Goes Here
Description: Text will be placed here and continue erat et tristique viverra, turpis purus tincidunt lacus, nec condimentum nisl neque sit amet augue. Ut tellus arcu, tempor ultrices.
- list item for group 02
- list item for group 02
- list item for group 02
- list item for group 02
----------------------------------------------------------------------
We look forward to your feedback!
studio-a
Re: [studio-a] Ordering by Multiple Value Lists
By Chris - December 11, 2010
Can you please post the code that you have working so far so I can see what your field names are, etc?
Chris
Re: [chris] Ordering by Multiple Value Lists
Thanks for asking. Below is the code we have been working on. The Group Title and List of Items work fine,
the problem is that we cannot figure out is how to include a Thumbnail Image for the Group Title along with
a Desciption for each Group.
What is happening - the same "thumbnail" and the same "description" is displayed for each Group Title
rather than having a separate image and description for each Group.
We suspect another array is needed?
<?php
list($local_attractionsRecords, local_attractionsMetaData) =getRecords(array(
'tableName' => 'attractions',
'perPage' => '10',
));
list($attractions_categoriesRecords, $attractions_categoriesMetaData) = getRecords(array(
'tableName' => 'attractions_categories',
));
$attractions_categories = @$attractions_categoriesRecords[0]; // get first record
?>
<?php
$orderedGroups=array();
foreach($local_attractionsRecords as $attractions){
$groups = trim($attractions['category'],"\t");
if($groups){ //record has groups
$groups = explode("\t",$groups);
foreach($groups as $group){
$orderedGroups[$group][]=$attraction;
}
}
else{ //record has no group
$orderedGroups['No Group'][]=$attraction;
}
}
?>
<!-- ////////// Thumbnail image for category ///////// -->
<?php foreach ($orderedGroups as $groupTitle => $group): ?>
<div>
<?php foreach ($attractions_categories['thumbnail'] as $upload): ?>
<?php if ($upload['hasThumbnail']): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" align="left" alt="alt tag goes here" />
<?php elseif ($upload['isImage']): ?>
<img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" align="left" border="0" alt=""/>
<?php endif ?>
<?php endforeach ?>
<!-- ////////// Title and Description per category ///////// -->
<?php if(is_numeric($groupTitle)): ?>
<h4><?php echo $categoryNumToName[$groupTitle]; ?></h4>
<?php else: ?>
<h2><?php echo $groupTitle; ?> </h2>
<?php endif ?>
<?php echo $attractions_categories['description'] ?> <br />
</div>
<!-- ////////// List of attractione per category ///////// -->
<?php foreach($group as $item): ?>
<li><?php echo $item['name'] ?>
<ul>
<p><?php echo $item['description'] ?></p>
</ul>
</li>
<?php endforeach ?>
<?php endforeach ?>
<!-- ////////// End Loop for Attractions Listing ///////// -->
Thanks for your help. We always appreciate your feedback!
studio-a
Re: [studio-a] Ordering by Multiple Value Lists
By Jason - December 13, 2010
The issue seems to be here:
$attractions_categories = @$attractions_categoriesRecords[0]; // get first record
This retrieves the first attractions_category record. Since this is the only record being used, you'll see the same thumbnail and description each time.
You can create a new array that organizes category records by num, so we can randomly access a category record:
list($attractions_categoriesRecords, $attractions_categoriesMetaData) = getRecords(array(
'tableName' => 'attractions_categories',
));
$categoryNumToRecord=array();
foreach($arractions_categoriesRecords as $categoryRecord){
$categoryNumToRecord[$categoryRecord['num']]=$categoryRecord;
}
You can now get at any individual category record based on it's num:
<?php foreach ($orderedGroups as $groupTitle => $group): ?>
<div>
<?php $attractions_categories = $categoryNumToRecord[$groupTitle];?>
<?php foreach ($attractions_categories['thumbnail'] as $upload): ?>
<?php if ($upload['hasThumbnail']): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" align="left" alt="alt tag goes here" />
<?php elseif ($upload['isImage']): ?>
<img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" align="left" border="0" alt=""/>
<?php endif ?>
<?php endforeach ?>
<!-- ////////// Title and Description per category ///////// -->
<?php if(is_numeric($groupTitle)): ?>
<h4><?php echo $categoryNumToName[$groupTitle]; ?></h4>
<?php else: ?>
<h2><?php echo $groupTitle; ?> </h2>
<?php endif ?>
<?php echo $attractions_categories['description'] ?> <br />
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] Ordering by Multiple Value Lists
Thanks for getting back to us. We removed the line for the single record as you mentioned, placed the array you wrote and added the line for
<?php $attractions_categories = $categoryNumToRecord[$groupTitle];?>
However, we're now getting errors Listed below:
Notice: Undefined index: SeaWorld(R) & Busch Gardens in /pages/attractionsTEST.php on line 127
Warning: Invalid argument supplied for foreach() in /pages/attractionsTEST.php on line 128
The code in reference is Listed below:
Line 127 <?php $attractions_categories = $categoryNumToRecord[$groupTitle];?>
Line 128 <?php foreach ($attractions_categories['thumbnail'] as $upload): ?>
We appreciate your help and love to work through this problem.
Thanks,
studio-a