Working With Multiple Multi-Select Results

11 posts by 2 authors in: Forums > CMS Builder
Last Post: May 5, 2011   (RSS)

By Perchpole - April 10, 2011

I've often seen posts in this forum demonstrating how to work with the tab-separated list of numbers produced by a multi-select menu. The resulting array can be used as a filter for a WHERE clause, such as...

$array = join( ",", explode( "\t", trim( $record['multi-select'], "\t" ) ) );

list($itemRecords, $itemMetaData) = getRecords(array(
'tableName' => 'table',
'allowSearch' => false,
'where' => "num IN ($array)",
));


I'm trying to construct a similar filter - but working with tab-separated numbers returned by multiple records.

I need to capture each sequence (one for each record), merge them (in such a way as to remove duplicate numbers) and use the resulting array as the basis of the filter.

Although I'm close to achieving the desired result, I cannot grasp at which point (and how) to merge the results and when to explode them.

Any help would be most appreciated.

:o/

Perch

Re: [Perchpole] Working With Multiple Multi-Select Results

By Jason - April 10, 2011

Hi Perch,

What you can do is loop through all of your records, turning each multi-select into a comma separated list and then string those lists together. It wouldn't matter if a number appeared twice in the list, as it would still only be selected once.

Try something like this:

<?php
recordNumList = "";

foreach($records as $record) {
$recordNumList .= join(",", explode("\t", trim($record['multi-select'], "\t"))) .",";
}

$recordNumList = rtrim($recordNumList, ",") // remove the last comma from the list

list($itemRecords, $itemMetaData) = getRecords(array(
'tableName' => 'table',
'allowSearch' => false,
'where' => "num IN ($recordNumList)",
));
?>

---------------------------------------------------
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] Working With Multiple Multi-Select Results

By Perchpole - April 11, 2011

Hi, Jason -

Thanks for your help with this. I was at the bang head against brick wall stage! Your solution is exactly what I was looking for.

Just as an aside, however, I wanted to see if I could use your code in a slightly different way (purely for the purpose of learning something).

I wanted to see if I could achieve the same result as the WHERE => "num IN ($array)" filter using php code that could be dropped straight into the page. I came up with this...

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

<?php $recordNumList = explode( "\t", trim( $record['multi-select'], "\t" ) ) ; ?>

<?php foreach ($itemRecords as $items): ?>
<?php if (!in_array($items['num'], $recordNumList)) { continue; } ?>
...
<?php endforeach ?>

<?php endforeach ?>


Does this do the same thing - or would you do it another way?

Thanks,

:0)

Perch

Re: [Perchpole] Working With Multiple Multi-Select Results

By Jason - April 11, 2011

Hi, Perch,

I see what you're going for. You're pretty close. You wouldn't be able to do this as a nested loop, since you need to populate $recordNumList with all of it's possible values before starting to filter records. Also, in your code you'd be outputting the values of $itemRecords multiple times.

What you would need to do is to first create an array called $recordNumList and then keep adding to it using array_merge. Once you have all you're values, you can use a separate loop to filter out the values of $itemRecords. Try something like this:

<?php $recordNumList = array(); ?>
<?php foreach($records as $record):?>

<?php $recordNumList = array_merge($recordNumList, explode( "\t", trim( $record['multi-select'], "\t" ) ) ) ; ?>

<?php endforeach ?>

<?php foreach ($itemRecords as $items): ?>
<?php if (!in_array($items['num'], $recordNumList)) { continue; } ?>
...
<?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] Working With Multiple Multi-Select Results

By Perchpole - April 11, 2011

Hi, Jason -

That makes perfect sense. I'll give it a go!

In the meantime, perhaps you can answer a quick question regarding foreach loops which is bugging me.

Is it possible to better define the loop before you set the thing in motion?

Often I see something like this...

<?php foreach ($insect as $bug): ?>
<? if($bug['type'] == "spider") {continue;} ?>
etc....


This always seems to me to be one step too many. Isn't it possible to say something along the lines of...

<?php foreach ($insect as $bug (except spiders)): ?>

...or some such thing?!

Just a thought.

:0)

Perch

Re: [Jason] Working With Multiple Multi-Select Results

By Perchpole - May 5, 2011

Hi, Jason -

Could you possibly give me some pointers regarding the creation of a simple function to do as you suggest above.
I've tried working through it myself but I suspect I'm going to spend a lot of time making a mess of it!

:0/

Perch

Re: [Perchpole] Working With Multiple Multi-Select Results

By Jason - May 5, 2011

Hi Perch,

Here is an example of how the function could work using all the same assumptions we were making above:

// function takes in array of bugs and returns array
// with spiders removed.
function filterOutSpiders($insects) {
$filteredArray = array();

foreach ($insects as $bug) {
if ($bug['type'] == "spider") { continue; }

$filteredArray[] = $bug;
}

return $filteredArray;
}


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] Working With Multiple Multi-Select Results

By Perchpole - May 5, 2011

A-ha! Thanks Jason -

I'm curious though, as to whether the function simply adds unnecessary code to the process of filtering out spiders from the original foreach loop!

Option 1
<?php foreach ($insects as $bug): ?>
<? if($bug['type'] == "spider") {continue;} ?>
etc....


Option 2
function filterOutSpiders($insects) {
$filteredArray = array();

foreach ($insects as $bug) {
if ($bug['type'] == "spider") { continue; }

$filteredArray[] = $bug;
}

return $filteredArray;
}

<?php foreach ( filterOutSpiders($insects) as $bug) : ?>
etc....



Both options achieve the same result - but one with rather less code than the other...

Wibble!

Perchpole

Re: [Perchpole] Working With Multiple Multi-Select Results

By Jason - May 5, 2011

Hi,

You're right, both would have the same effect. For this particular example, option 1 is probably the better bet since we're only doing a simple filter. But, if you needed to do more complex filtering, sometimes it cleaner to hide the filtering in a separate function.

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/