Calculating the selected prices in a listbox

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


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-13-2008
mail@blueguminteractive.biz
 
Posts: n/a
Default Calculating the selected prices in a listbox

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.
Reply With Quote
  #2 (permalink)  
Old 01-13-2008
Daniel
 
Posts: n/a
Default Re: Calculating the selected prices in a listbox

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.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 07:56 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0