Can I access Dropdown list option text?

Permalink Browser Info Environment
Hi Jordan

I created a dropdown list with 3 options that I want to output depending on what was chosen. Can you tell me if there is an easy way to do that?

e.g.
Options:
- Height
- Length
- Depth

If someone chooses the first option, I just want to out the exact option text "Height".

By default, view.php has:

<?php if ($field_1_select_value == 1): ?>
<!-- ENTER MARKUP HERE FOR FIELD "Unit 1" : CHOICE "Height" -->
<?php  endif; ?>

etc... for each option

I don't really want to have to retype the option text for each item, so was hoping I could just use a variable like: $field_1_select_option

Is a variable like this available?

Thank you!

Type: Discussion
Status: New
malkau
View Replies:
malkau replied on at Permalink Reply
malkau
I came up with my own quick hack, but was wondering if there was a better way...

I noticed this code in the edit.php:

<h2>Unit</h2>
<?php 
$options = array(
   '0' => '--Choose One--',
   '1' => 'Height',
   '2' => 'Length',
   '3' => 'Width',
);
echo $form->select('field_1_select_value', $options, $field_1_select_value);
?>


So I used this code to create option lists in my view.php file:

// unit options
$unit_options = array(
   '0' => '--Choose One--',
   '1' => 'Height',
   '2' => 'Length',
   '3' => 'Width',
);


then later in my HTML code:

<th><?php  echo $unit_options[$field_1_select_value] ?></th>
<td><?php  echo htmlentities($field_2_textbox_text, ENT_QUOTES, APP_CHARSET); ?></td>


It works... but I was hoping there might be a better way to do this :)

Thanks
- Mal
jordanlev replied on at Permalink Reply
jordanlev
Hi,
I think the method you came up with is just fine. The dropdown field is not very robust -- it's goal was just to allow the user to choose from a few different design options, not to actually serve as a way to output certain data.

One thing you might want to do though to reduce code duplication (so if you change the list in the future you won't accidentally forget to change it in one place but not the other) is add a new function to the controller that returns that array. Then in edit.php and view.php call the function to get the array.

For example, the controller function would look like this:
public function getUnitOptions() {
    return array(
        '0' = '--Choose One--',
        '1' => 'Height',
        '2' => 'Length',
        '3' => 'Width',
    );
}


Then in the edit and view.php file you can do this:
$unit_options = $controller->getUnitOptions();


Hope that helps!

-Jordan
malkau replied on at Permalink Reply
malkau
Thanks Jordan! I will do that!

I saw your post about Designer Content Pro - I look forward to hearing more about the features. The repeating items sounds handy as I have used your block for quite of few lists of items (products, categories, testimonials) and I've had to kind of compromise my ideal HTML code (e.g. when I've wanted to us UL for lists) and do crazy things with stacks and layouts to make the content reusable.

Cheers!

concrete5 Environment Information

Browser User-Agent String

Hide Post Content

This will replace the post content with the message: "Content has been removed by an Administrator"

Hide Content

Request Refund

You have not specified a license for this support ticket. You must have a valid license assigned to a support ticket to request a refund.