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 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

Greg Thomas







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

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php