Welcome Guest, Not a member yet? Register   Sign In
Form_Dropdown Extra Values to Options
#1

[eluser]elmbrent[/eluser]
Hi,

I have been pulling my hair out for god knows how long trying to work this out.

Currently I have a select list (form_dropdown) like this.

Code:
<select name="starters"  none;">
<option value="">Please Select</option>
<option value="0" >Brochettes</option>
<option value="1" >Grilled mackerel</option>
<option value="2" >Risotto cake</option>
<option value="3" >Calamari rings</option>
<option value="4">Parma ham parmesan</option>
</select>

What I need to do Is pass into this form_dropdown is a Price Value, so I can have a dynamic price calculator on the website.

Code:
<select name="starters"  none;">
<option value="" price="100">Please Select</option>
<option value="0" price="22">Brochettes</option>
<option value="1" price="342">Grilled mackerel</option>
<option value="2" price="42">Risotto cake</option>
<option value="3" price="52">Calamari rings</option>
<option value="4" price="22">Parma ham parmesan</option>
</select>

Is there any way I can do this? Im really stuck for dead lines and I thought this would be possible in code igniter.
#2

[eluser]CroNiX[/eluser]
Yes, but not by default. You'd have to extend the form_dropdown() helper to make it do what you want. It only works with an array of key => value pairs currently.
#3

[eluser]elmbrent[/eluser]
Hi, Thanks for the reply Smile, would you please be able to post a example on how I would do this?

#4

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums!

Since price is not a valid <option> attribute, perhaps it would make more sense to include the price in the option tags themselves, and then extract the prices with your calculator? I'm not sure if any browsers might remove it from the mark-up since it's invalid.

Alternatively, you may want to consider inserting a JSON array into your mark-up, which contains your prices, so your calculator can pull the prices from that.
#5

[eluser]elmbrent[/eluser]
How could I combine both arrays then?

So it would show ( Product Name - Price )

Code:
echo form_label("APPETISERS", "appetisers");
    $array = array();
    foreach($appertisers as $row ){
         $array[] = $row->product;
      $array[] = $row->price;
    }
    $array = array_merge(array('' => 'Please Select'), $array);
    echo form_dropdown('appetisers',  $array);


I could then use RegEx to pull out the price by taking the last numbers of the selected value.


#6

[eluser]TheFuzzy0ne[/eluser]
[quote author="elmbrent" date="1363624993"]How could I combine both arrays then? [/quote]

You could use the ID of the item as your <option> value. Then you can get the ID of the selected item, and grab the price from the JSON array.


[quote author="elmbrent" date="1363624993"]So it would show ( Product Name - Price )

Code:
echo form_label("APPETISERS", "appetisers");
    $array = array();
    foreach($appertisers as $row ){
         $array[] = $row->product;
      $array[] = $row->price;
    }
    $array = array_merge(array('' => 'Please Select'), $array);
    echo form_dropdown('appetisers',  $array);
[/quote]

I was thinking something more along the lines of:
Code:
$opts = array('' => 'Please Select');
foreach($appertisers as $row ){
    // The format can be changed as appropriate.
    $opts[$row->id] = $row->product.' ('.$row->price.')';
}
echo form_dropdown('appetisers', $opts);

[quote author="elmbrent" date="1363624993"]
I could then use RegEx to pull out the price by taking the last numbers of the selected value.
[/quote]

Indeed. Smile
#7

[eluser]StevenSokulski[/eluser]
I did this recently using the data attributes. If you add data-price="4.99" to the tag the browsers will leave it alone and your JS can pull it out and use it in calculations.

Code:
<select name="option-color" id="option-color" class="product-option">
<option value="Red" data-price="0">Red</option>
<option value="Green" data-price="5">Green (+$5)</option>
</select>
#8

[eluser]elmbrent[/eluser]
Thanks for solutions guys, they really helped. What an awesome community!




Theme © iAndrew 2016 - Forum software by © MyBB