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

Correction: almost fixed :(

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

So I have 2 issues with the above after more testing.

Issue 1: I would like to be able to sort the sizes showing the Alphabetical content first followed by the Alphanumeric

Issue 2 (the most serious): When I specify the size in the drop down and the search is done. The size drop down then lists ALL the sizes available from the items listed. I would like it to show ONLY the size selected initially. This is important for another reason. If a brand has both bras and briefs, and the user selects bras, then the resulting output show bras for all brands, not just for the brand selected initially.



Any ideas?

Thanks

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