This is a discussion on Calculating the selected prices in a listbox within the PHP General forums, part of the PHP Programming Forums category; I am wanting to know how I can add the all of the selected prices in a listbox? I need ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I am wanting to know how I can add the all of the selected prices in a
listbox? I need to add the selected prices altogether. The listbox should have only the names of the products. The listbox should be like below: <select name="powertoolslist[]" size="5" multiple="multiple" id="powertoolslist"> <option value="Product 1">Product 1 - $16 per year</option> <option value="Product 2">Product 2 - $103 per year</option> <option value="Product 3">Product 3 - $89 per year</option> <option value="Product 4">Product 4 - $16 per year</option> <option value="Product 5">Product 5 - $16 per year</option> <option value="Product 6">Product 6 - $260 per year</option> <option value="Product 7">Product 7 - $16 per year</option> <option value="Product 8">Product 8 - $22.50 per year</option> <option value="Product 9">Product 9 - $16 per year</option> <option value="Product 10">Product 10 - $102 per year</option> </select> Can someone please help me with this? Thanks in advance. |
|
|||
|
mail@blueguminteractive.biz wrote:
> I am wanting to know how I can add the all of the selected prices in a > listbox? I need to add the selected prices altogether. The listbox > should have only the names of the products. The listbox should be like > below: > > <select name="powertoolslist[]" size="5" multiple="multiple" > id="powertoolslist"> > <option value="Product 1">Product 1 - $16 per year</option> > <option value="Product 2">Product 2 - $103 per year</option> > <option value="Product 3">Product 3 - $89 per year</option> > <option value="Product 4">Product 4 - $16 per year</option> > <option value="Product 5">Product 5 - $16 per year</option> > <option value="Product 6">Product 6 - $260 per year</option> > <option value="Product 7">Product 7 - $16 per year</option> > <option value="Product 8">Product 8 - $22.50 per year</option> > <option value="Product 9">Product 9 - $16 per year</option> > <option value="Product 10">Product 10 - $102 per year</option> > </select> > > Can someone please help me with this? Thanks in advance. You need to add the cost into the key for each product. You could do this easily by using the keyname as a serialized array (users dont see key names unless they nose the source, it doesnt matter what it looks like. <?php $products = array( 'Product 1' => 16, 'Product 2' => 103, 'Product 3' => 89, 'Product 4' => 16, 'Product 5' => 16, 'Product 6' => 260, 'Product 7' => 16, 'Product 8' => 22.50, 'Product 9' => 16, 'Product 10' => 102 ); echo '<select name="powertoolslist" size="5" multiple="multiple" id="powertoolslist">'; foreach($products as $name => $price) { echo '<option value="' . serialize(array('name' => $name, 'price' => $price)) . '">' . $name . ' - $' . $price . ' per year</option>'; } echo '</select>'; ?> Then to process it. <?php $total = 0; foreach($_POST['powertoolslist') as $r) { $array = unserialize($r); $total += $array['price']; } echo "Total: {$total}"; ?> That should give you a good idea. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|