Options in a form populated only from checked values in a field
3 posts by 2 authors in: Forums > CMS Builder
Last Post: July 23, 2013 (RSS)
By gkornbluth - July 23, 2013
Hi All,
I’m trying to set up a form field on a detail page paypal form that will allow a buyer to choose the color of an item from only those colors available for that particular item.
The colors available are in a multi-value check box in the item's record. (Possible values are from another database using the "record number" for the value and a" title" field value for the label)
Right now I’m using the following code, and it lists all available colors in the form, whether checked as available or not.
I've mucked about but can’t seem to figure out how to list only the colors that are checked as available in the particular record.
<select name = "os1" >
<option value="">Please Choose a Color</option>
<?php foreach (getListOptions('store_inventory', 'colors') as $value => $label): ?>
<option value = "<?php echo $label;?>" <?php selectedIf($value, @$_REQUEST['colors']);?>>
<?php echo $label; ?></option>
<?php endforeach ?>
</select>
Thanks,
Jerry Kornbluth
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
By gregThomas - July 23, 2013
Hi Jerry,
Are you using a getRecords function to retrieve the details for the product? If so the values and labels selected for that product are included in getRecords array, you could select the values and labels like this:
// load record from 'blog'
list($blogs, $blogMetaData) = getRecords(array(
'tableName' => 'blog',
'where' => "`num` = '1'",
'loadUploads' => true,
'allowSearch' => false,
'limit' => '1',
));
$blog = $blogs['0'];
//Create a drop down list by combining the values and labels into one array
$dropList = array_combine($blog['category:values'], $blog['category:labels']);
?>
<select name="drop">
<!-- cycle through drop list to create options -->
<?php foreach($dropList as $value => $label): ?>
<option value="<?php echo $value; ?>"><?php echo $label; ?></option>
<?php endforeach; ?>
</select>
This is just example code, so you'll have to apply the method to your products system.
So in my example I have a list field in my blog section that is linked to another section (called blog_categories) using this method: http://www.interactivetools.com/kb/article.php?Populate-a-list-field-from-another-section-15 . The list field is using the num as the value and the title for the label. In the code above I'm getting the data for a blog record using the getRecords function. Then I'm creating a variable called $dropList that stores an array of blog categories values and labels using the array_combine function. Finally the code cycles through this list using a foreach loop to create a select input.
Let me know if you have any questions.
Thanks
Greg
PHP Programmer - interactivetools.com
By gkornbluth - July 23, 2013
Hi Greg,
Thanks for your suggestions. The page works perfectly now and I've created a recipe in my CMSB Cookbook so that others can learn from the master (that's you).
The final code for selecting available colors in the PayPal "Buy Now" button ended up being:
<input type="hidden" name="on1" value="Color">
<?php // load record from 'store_inventory'
list($colors, $store_inventoryMetaData) = getRecords(array(
'tableName' => 'store_inventory',
'where' => whereRecordNumberInUrl(1),
'loadUploads' => true,
'allowSearch' => false,
'limit' => '1',
));
?>
<?php $available_colors = $colors['0'];
//Create a drop down list by combining the values and labels into one array
$dropList = array_combine($available_colors['colors:values'], $available_colors['colors:labels']);
?>
<select name="os1">
<option value="">Please Choose a Color</option>
<!-- cycle through drop list to create options -->
<?php foreach($dropList as $value => $label): ?>
<option value = " <?php echo $label; ?>" <?php selectedIf($value, @$_REQUEST['colors']);?>> <?php echo $label; ?></option>
<?php endforeach; ?>
</select>
We certainly couldn't accomplish what we do without your expert guidance.
Best,
Jerry Kornbluth
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php