Simple Sub-array Filter via List Selection

7 posts by 3 authors in: Forums > CMS Builder
Last Post: February 25, 2011   (RSS)

Hello, All -

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

Hi, Jerry -

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

Hi Perch,

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

Ah. That's it! Thanks Jason. I knew I was close...

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

Hi Perch,

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

Jason -

Brilliant. Just what I wanted.

Thanks,

Perch