Filter multiple sizes result

6 posts by 2 authors in: Forums > CMS Builder
Last Post: April 26, 2011   (RSS)

Greetings all :)

I have a section for clothing on a website. Each item can have multiple sizes specified via radio buttons. For example: 32A, 32C, and so on.

In the list page I have created, I am using drop-downs to show what is available. I have other elements such as brand, and colours working fine.

However with the multiple choice sizes, the drop-down list that is generated, each line has multiple sizes listed like this:

32AA 34C 36C 38B
30D 32C 32D 34A 34B 34C 36A 36B 36C 36D 38B

I would like to be able to merge the sizes so that duplicates are removed and each entry is turned into a drop-down option. For example:

30D
32AA
32C

Here is the code I am currently using:


<?php

// load records
list($clothingRecords, $clothingMetaData) = getRecords(array(
'tableName' => 'clothing',
));


foreach ($clothingRecords as $record):
$sizeRecords[] = $record['size'];
$sizeRecords = array_unique($sizeRecords);
endforeach;
?>


<select name="size" >
<option value="">All</option>
<?php foreach ($sizeRecords as $record): ?>
$record
<option value="<?php echo htmlspecialchars($record) ?>"><?php echo htmlspecialchars($record) ?></option>
<?php endforeach; ?>
</select>


Any suggestions?

Thanks in advance

Paul

Re: [Pixels & Beats] Filter multiple sizes result

By robin - April 25, 2011

Hey Paul,

If your current code is not working, you can try using array_unique after the loop and remove all duplicates at once. Something like this:

foreach ($clothingRecords as $record) {
$sizeRecords[] = $record['size'];
}
$sizeRecords = array_unique($sizeRecords);


Hope that helps,
Robin
Robin
Programmer
interactivetools.com

Re: [robin] Filter multiple sizes result

By gadefgaertgqe - April 25, 2011 - edited: July 17, 2012

Hello Robin :)

I tried the code you suggested but it made no difference. To be honest I may not be explaining myself very well.


If you look at the sizes drop-down it lists they like this:
Selection 1: 32AA 34C 36C 38B
Selection 2: 30D 32C 32D 34A 34B 34C 36A 36B 36C 36D 38B

I understand why it's presented like this, because of the way I specify the sizes (I have attached an image of how they are specified), but what I need to do so that the search function works, is for all the size options generated to be listed as individual options, with any duplicates removed, so that the drop-down shows:

30D
32AA
32C
34A
34B
34C
36A
36B
36C
36D
38B

I know I need to create a new array with them sorted but I can't seem to quite get it. Hope this makes better sense lol!

Thanks :)

Paul

FIXED!!!

By gadefgaertgqe - April 26, 2011 - edited: April 26, 2011

OK, thought I would share this as you never know who will end up with the same problem :)

There were a number of odd factors that I found out that I was dealing with. For example spurious /t (tab) characters etc. Any way here is a documented breakdown of what I did to resolve the problem:

<?php foreach ($clothingRecords as $record):

$sizeRecords[] = $record['size'];
$sizeRecords = array_unique($sizeRecords);

endforeach;

$sizeRecords2 = join("",$sizeRecords); // merges arrays into a string

$sizeRecords2 = str_replace ("\t", ",", $sizeRecords2); // removes /t (tab) from string (not sure how it gets in there), and replaces with comma

$sizeRecords2 = str_replace (",,", ",", $sizeRecords2); // removes the double comma that is created by the above function because of double /t (tab) in between each new array that is merged

$sizeRecords2 = (substr($sizeRecords2,-1) == ',') ? substr($sizeRecords2, 0, -1) : $sizeRecords2; // removes the last comma on end of string

$sizeRecords2 = ($sizeRecords2[0] == ',') ?

substr($sizeRecords2, 1) : $sizeRecords2; // removes the first comma on start of string

$sizeRecords3 = explode(',', $sizeRecords2); // Explodes string into an array

$sizeRecords4 = array_unique($sizeRecords3); // Removes Duplicates from array

asort($sizeRecords4); // Sorts array into numeric order

?>


There is probably a more efficient way to do this, but I am still proud of myself for getting to this stage.

...and again thanks to Robin for his input :)

Paul

Re: [Pixels & Beats] Correction: almost fixed :(

By robin - April 26, 2011

Hey,

1.
It's a bit advanced, but you can do a custom sort with usort()
http://www.php.net/manual/en/function.usort.php

2.
CMS Builder has a handy function called selectedIf which will let you test if a menu should have a default value. So in the code that builds your size menu, something like this should help:
<select name="size" >
<option value="">All</option>
<?php foreach ($sizeRecords as $record): ?>
$record
<option value="<?php echo htmlspecialchars($record) ?>" <?php selectedIf($record, $_REQUEST['size']); ?> ><?php echo htmlspecialchars($record) ?></option>
<?php endforeach; ?>
</select>

Hope that helps,
Robin
Robin
Programmer
interactivetools.com