Simple Sub-array Filter via List Selection
7 posts by 3 authors in: Forums > CMS Builder
Last Post: February 25, 2011 (RSS)
I hope this is a simple but useful request...
I have a group of records. Whilst creating each record the user is required to select a colour from a drop-down list.
I now wish to filter these records into sub-arrays according to the selected colour.
I assumed it would work with code something like this:
$colourRed = array();
$colourWhite = array();
$colourBlue = array();
foreach($Records as $selection){
if($selection['colour'] == "red"){
$colourRed[]=$selection['colour'];
}
if($selection['colour'] == "white"){
$colourWhite[]=$selection['colour'];
}
if($selection['colour'] == "blue"){
$colourBlue[]=$selection['colour'];
}
}
For some reason this doesn't quite work - but you get the idea!
Any suggestions would be most welcome.
:0)
Perch
Re: [Perchpole] Simple Sub-array Filter via List Selection
I don't know it this recipe from my CMSB Cookbook http://www.thecmsbcookbook.com will help, but it might give you some ideas.
Jerry Kornbluth
SORTING BY GROUP AND INSERTING GROUP HEADINGS IN A LIST VIEWER
If you’d like to display group headings on your list page here’s one approach:
For this example, a multi-record editor, called “events” has the following fields:
Title (a text field)
Type (a pull down list of “Groups” to minimize entry errors)
Starting Date (a date field) * in the viewer, only the Month, Day and Year are visible
Preview (a short description of the event for the listing page)
In your application, you could add a full description of the event for the details page, images, etc.
The sort is by Type (ASC)ending and then by Start Date (ASC)ending
On the list viewer page the following code appears where you want your list to appear .
<?php
$old_group = ''; // init blank var.
foreach ($eventsRecords as $record):
$group = $record['type']; // load sub-group value from record.
if ($group != $old_group) { // If different from the last sub-group value, print the sub-group name.
echo "<h2>$group</h2>";
}?>
<a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a>
<?php echo $record['preview'] ?>
<br /><br />
<?php $old_group = $group; // retain sub-group name before moving to new record. ?>
<?php endforeach ?>
If you want to use alternate text or graphics to represent your groups you could use the following.
For Text:
<?php
$old_group = ''; // init blank var.
foreach ($eventsRecords as $record):
$group = $record['type']; // load sub-group value from record.
if ($group != $old_group && $record['type'] == "Group 1") { // If different from the last sub-group value, and equals Group 1, print the sub-group name.
echo "<h2>Group 1 text </h2>";}
if ($group != $old_group && $record['type'] == "Group 2") { echo "<h2>Group 2 text</h2>";}
if ($group != $old_group && $record['type'] == "Group 3") { echo "<h2>Group 3 text</h2>";}
if ($group != $old_group && $record['type'] == "Group 4") { echo "<h2>Group 4 text</h2>";}
?>
<a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a>
<?php echo $record['preview'] ?>
<br /><br />
<?php $old_group = $group; // retain sub-group name before moving to new record. ?>
<?php endforeach ?>
To display images instead of text for the headings, one approach would be to create a separate single record editor called “Graphics” with each image as a separate upload field named Group 1 Graphic, Group 2 Graphic, etc., and use something like this on the viewer page (don’t forget to add the getrecord call at the top of your page).
<?php $old_group = ''; ?>
<?php foreach ($eventsRecords as $record): ?>
<?php $group = $record['type']; ?>
<?php if ($group != $old_group && $record['type'] == "Group1"): ?>
<div align="center"><?php foreach ($graphicsRecord['group_1_graphic'] as $upload): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt='' /><br /><br /></div>
<?php endforeach ?> <?php endif ?>
<?php if ($group != $old_group && $record['type'] == "Group 2"): ?>
<div align="center"><?php foreach ($graphicsRecord['group_2_graphic'] as $upload): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt='' /><br /><br /></div>
<?php endforeach ?> <?php endif ?>
<?php if ($group != $old_group && $record['type'] == "Group 3"): ?>
<div align="center"><?php foreach ($graphicsRecord['group_3_graphic'] as $upload): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt='' /><br /><br /></div>
<?php endforeach ?> <?php endif ?>
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Re: [Perchpole] Simple Sub-array Filter via List Selection
Thanks for your input.
I've often employed your suggested code - which has a number of uses.
On this occasion, however, it's not quite what I need. I want to set things up more along the lines of my own code above (even though it's wrong!)
:0)
Perch
Re: [Perchpole] Simple Sub-array Filter via List Selection
By Jason - February 24, 2011
So in each of your three colour arrays, you want to store all the records from your section that have that colour selected. Is that right?
If so, you're remarkably close. In your code, you're only storing the value of the colour field in your array instead of the entire record. Try changing your code to this:
$colourRed = array();
$colourWhite = array();
$colourBlue = array();
foreach($Records as $selection){
if($selection['colour'] == "red"){
$colourRed[] = $selection;
}
if($selection['colour'] == "white"){
$colourWhite[] = $selection;
}
if($selection['colour'] == "blue"){
$colourBlue[] = $selection;
}
}
After this, you can use each of these arrays as if they had been selected directly from the database. For example:
<?php foreach($colourBlue as $record) : ?>
...
<?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] Simple Sub-array Filter via List Selection
OK. Seeing as how we polished that one off so quickly, let's take it to the next logical step.
What would the code be if the user could choose more than one colour from a multi-select list?
:0)
Perch
Re: [Perchpole] Simple Sub-array Filter via List Selection
By Jason - February 25, 2011
What we'd need to do is break up the multiple value list into an array and then check for different values. Note in this example, if a selection has more than 1 colour, the record will appear in more than 1 place.
$colourRed = array();
$colourWhite = array();
$colourBlue = array();
foreach($Records as $selection){
$colours = explode( "\t", trim( $selection['colour'], "\t" ) );
if( in_array( "red", $colours ) ){
$colourRed[] = $selection;
}
if( in_array( "white", $colours ) ){
$colourWhite[] = $selection;
}
if( in_array( "blue", $colours ) ){
$colourBlue[] = $selection;
}
}
This would work the same if you were using a multivalue drop down or a multi-value check boxes.
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] Simple Sub-array Filter via List Selection
Brilliant. Just what I wanted.
Thanks,
Perch