Forum Moderators: coopster

Message Too Old, No Replies

calculation of items in drop down menu

         

Flolondon

10:03 am on Mar 31, 2006 (gmt 0)

10+ Year Member




Hello all.

I was just wondering what method is best to use to calculate items from a drop down menu.
I have two lots of drop down menu of which i want to
send to a page to clarification before being submitted.
thanks

scriptmasterdel

12:34 pm on Mar 31, 2006 (gmt 0)

10+ Year Member



Can you explain more on what you mean, maybe show some code of an example?

Del

Flolondon

1:24 pm on Mar 31, 2006 (gmt 0)

10+ Year Member



hello del.
Well what i am trying to do is to add to variables together and formulate an overall price to output within html

dreamcatcher

1:29 pm on Mar 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<select name="prices[]" multiple>
<option>1.00</option>
<option>1.00</option>
<option>1.00</option>
<option>1.00</option>
<option>1.00</option>
</select>

<?php

$total = 0;

if (!empty($_POST['prices']))
{
foreach($_POST['prices'] as $cost)
{
$total = $total+$cost;
}
}

echo 'Final Total is: $'. $total;

?>

Something like that should be ok. Assuming you are using a multi select box?

dc

Flolondon

1:40 pm on Mar 31, 2006 (gmt 0)

10+ Year Member



thanks for that...

well i am using single drop down menu not a multiple select.

say for example i have drop down menu of different clothes
and on an invoice form i want it to show the price and delivery of each of the different clothes selected by the user

scriptmasterdel

2:07 pm on Mar 31, 2006 (gmt 0)

10+ Year Member



i think there is a function called
array_sum()
that counts all the variables in an array.

Then if you add

number_format()
to two decimal places that will also do the trick! ... i think

Flolondon

2:39 pm on Mar 31, 2006 (gmt 0)

10+ Year Member



$brush_price = 5;
$counter = 10;

echo "<table border=\"1\" align=\"center\">";
echo "<tr><th>Quantity</th>";
echo "<th>Price</th></tr>";
while ( $counter <= 100 ) {
echo "<tr><td>";
echo $counter;
echo "</td><td>";
echo $brush_price * $counter;
echo "</td></tr>";
$counter = $counter + 10;
}
echo "</table>";

HELLO dell.. say for example the above and you had multiple variables above instead of just 2; say you had 9 variables with = to a value sign.
and depending on which one was picked by the user - would output different prices

scriptmasterdel

3:03 pm on Mar 31, 2006 (gmt 0)

10+ Year Member



Display the total cost by the amount of "products" selected?

<?
// set the vars
$brush_price = 5;
$counter = 10;
echo "<form>";
echo "<table border=\"1\" align=\"center\">";
echo "<tr><th>Quantity</th>";
echo "<th>Price</th>";
echo "<td> &nbsp; </td>";
echo "</tr>";
// do a for loop fot the array
for($count=0;$counter <= 100; $count+10)
{
echo "<tr><td>";
echo $counter;
echo "</td><td>";
$value = $brush_price * $counter;
echo $value;
// set the value for the checkbox to send the request
$val2 = $counter*$value;
echo "</td>";
// add checkbox
echo "<td><input type='checkbox' value='$val2' name='counter[]'></td>";
echo "</tr>";
$counter = $counter + 10;
}

echo "<tr><td align=\"center\" colspan=\"3\">";
// submit button
echo "<input type='submit' name='subForm' value=\"calculate\">";
echo "</td></tr>";
echo "</table>";
echo "<form>";

// show the outcome of all selected items?
if(!empty($_REQUEST['subForm']) &&!empty($_REQUEST['counter']))
{
echo 'Your total Cost is: £'.array_sum($_REQUEST['counter']);

}
?>

Is this kind of what you are looking for?