Check boxes results shown as a drop down menu

13 posts by 2 authors in: Forums > CMS Builder
Last Post: September 28, 2010   (RSS)

By Zicky - July 9, 2010

Web page drop down menu

<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<select name="title,content,flavor_query">
<option value="">jellybean flavors</option>
<?php foreach ($jellybeansRecords as $record): ?>
<option value="<?php echo htmlspecialchars($record['flavor']) ?>"><?php echo htmlspecialchars($record['flavor']) ?></option>
<?php endforeach; ?>
</select><input name="submit" type="submit" class="flavorClass" value="go">
</form>

CMS Builder
Field Type: List
Field Name: flavor
Display as: "checkboxes (multi value)"
List options:
grape
grape ape
grape burst
sour grape
grape swirl


I have created a check box list that allows my client to check the various flavor combinations available per jellybean product listing, so that when the specific flavors combinations are selected in the check box list it appears in the pop down selection list form shown in the above code. The code above works fine... however if there is more than one item selected in the check box list when creating a new record the list options become one long menu selection (example: grape grape ape grape burst sour grape grape swirl) in the pop down list and I need to keep each checked item as a separate list item in the drop down menu so it would be as the example below:
grape
grape ape
grape burst
sour grape
grape swirl

Can someone explain how to display multiple check box list items as separate selections within a drop down menu using code similar to that shown above?

Thanks!

Re: [zickey] Check boxes results shown as a drop down menu

By Jason - July 12, 2010

Hi,

CMS Builder stores multi-value fields as tab separated strings. So what we need to do is for each "flavor" field break the string down into an array of values. Try this code:

<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<select name="title,content,flavor_query">
<option value="">jellybean flavors</option>
<?php foreach ($jellybeansRecords as $record): ?>
<?php
$flavors= $record['flavor'];
$flavors=trim($flavors,"\t");
$flavors=explode("\t",$flavors);
?>
<?php foreach ($flavors as $flavor): ?>
<option value="<?php echo $flavor; ?>"><?php echo $flavor;?></option>
<?php endforeach ?>
<?php endforeach; ?>
</select><input name="submit" type="submit" class="flavorClass" value="go">
</form>


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] Check boxes results shown as a drop down menu

By Zicky - July 13, 2010

Hey Jason, thanks for the help! I've got it plugged into the site and it's working... very helpful!

Got another question for you,

The drop down only shows the $flavors associated with the records that are presently shown on the list page.

For example, I have five records under the $jellybeansRecords and I have my list page set to display only the first three records. To see the other two records, you need to click the NEXT button at the bottom of the list page. So when you're viewing the first three records on the list page... the drop down menu only returns the values associated with those first three records (grape, grape ape, grape burst) displayed on the list page. Then once you click the NEXT bottom to view the next list page, the drop down menu returns only the values associated with the last two records (sour grape, grape swirl) displayed on the list page.

Is it possible to make all the check boxes as a drop down menu display in the code you provided regardless of whether your on the first list page, second, third, forth, etc. list page? This would ensure that if someone is viewing the second, third, forth, etc. list page, they'd still be able to chose a flavor from the previous list page in the drop down menu.

Field Type: List
Field Name: flavor
Display as: "checkboxes (multi value)"
List options:
grape
grape ape
grape burst
sour grape
grape swirl

Thanks for your help with this!
Zickey

Re: [zickey] Check boxes results shown as a drop down menu

By Jason - July 13, 2010

Hi Zickey,

Yes, this can be done. The easiest way would be to use a second getRecords statement that didn't limit the number of results, just collected all of them. You would then use your $jellybeansRecords in your foreach loop that displayed your paged results, but your second recordset in the foreach loop that creates the drop down box. In this way, the drop down box won't be affected by which page the user is on.

Give this a try. If you run into any trouble, just let me know and attach a copy of the .php file that you're working with.

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: [zickey] Check boxes results shown as a drop down menu

By Jason - July 14, 2010

Hi,

It looks good.

Let me know if there are any other issues you run into.
---------------------------------------------------
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] Check boxes results shown as a drop down menu

By Zicky - July 14, 2010

Hey Jason,
I just tested it out on the site and it works great... no matter what listing page your on all the drop down keywords are shown and when you click into one of the drop down menu items it limits only the product related keywords based on the previous selection's results... thereby creating a nice drill down feature that can be used to narrow down the selections based on user selections.

Thanks a ton for your help! This is exactly what we needed!
Zickey

Re: [Jason] Check boxes results shown as a drop down menu

By Zicky - September 24, 2010

Jason,

When I create multiple records that have the same checkboxes selected (see record check box examples 1) below) the drop down menu presently returns a checkbox list item twice 2). Is there a way to make the drop down menu ignore duplicate check box list items and only return one 3)... yet when someone uses the drop down menu and selects "Grape" then both records would then be returned as the results of the selection of Grape?

1) Record one check box list selections:
grape
strawberry

Record two check box list selections:
grape
banana

2) Present drop down menu item returned values:
grape
strawberry
grape
banana

3) Desired results for drop down menu:
grape
strawberry
banana

So I hope this makes sense... I think I've even confused myself.

Re: [zickey] Check boxes results shown as a drop down menu

By Jason - September 24, 2010

Hi,

I think I see what you're getting at. You just want to make sure the same flavor isn't being added to the drop down multiple times.

Try this code: (I've highlighted the changes in red)

<?php $currentFlavors=array();?>
<?php foreach ($jellybeansFlavorsRecords as $record): ?>
<?php
$flavors= $record['flavor'];
$flavors=trim($flavors,"\t");
$flavors=explode("\t",$flavors);
?>
<?php foreach ($flavors as $flavor): ?>
<?php if(!in_array($flavor,$currentFlavors)):?>
<option value="<?php echo $flavor; ?>"><?php echo $flavor;?></option>
<?php $currentFlavors[$flavor]=$flavor; ?>
<?php endif ?>

<?php endforeach ?>
<?php endforeach; ?>


This code just maintains an array of all of the flavors that have already been outputted, so if it finds one twice, it will just skip it.

Give that a try and let me know if you run into any other issues.

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] Check boxes results shown as a drop down menu

By Zicky - September 24, 2010

Hey Jason,
Worked perfectly!!! Thanks for the help.