homepage Welcome to WebmasterWorld Guest from 174.129.163.183
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe and Support WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
calculating from a dynamic form
calculating forms math arrays
sades




msg:3878477
 2:47 pm on Mar 25, 2009 (gmt 0)

Hello all im new to the forum and kind of new to php im trying to do this but cant figure out how

i have a page with a dynamic form wich extracts info from a table

id
title
price

on the form i have a repeat all which will display the title, price and id of each item, and a text box which i added id= <?php $id; ?> and also the internal name this text box will ask the user for an amount what i need to do is that this amount be multiply by the price and then do a sum of all of it, i have manage to show the amount in a $_POST[$id] but i have no idea how to do the multiplication and then the sum, i donīt need to save the data from the post in a database just show the person that is using this what he has to pay if he adds X amounts of products, i was thinking on using arrays but im still to new to php trying to learn any helps is appreciated

Thanks

 

wheelie34




msg:3878585
 4:55 pm on Mar 25, 2009 (gmt 0)

Hi sades and welcome to the forum

Maths is fairly simple in php, for example

$answer = ( $a x $b );# multiply
$answer = ( $a - $b );# subtract
$answer = ( $a + $b );# add
$answer = ( $a / $b );# divide

HTH

d40sithui




msg:3878633
 5:46 pm on Mar 25, 2009 (gmt 0)

You need to save your item id, and your item amount(which the user enters) in an array. Then loop through the array, find the price for each item (using a query with the item id stored), and then multiply that with the amount - also stored in the array.

sades




msg:3878644
 5:53 pm on Mar 25, 2009 (gmt 0)

thanks for the replays on the math part i have no problems what i dunno how to do is save the variable in the post (amount) in an arrays and then do the amount*price thing for each element of the array any tips?

whoisgregg




msg:3878656
 6:12 pm on Mar 25, 2009 (gmt 0)

Btw, multiplication uses the asterisk "*" not the lower case "x" ;)

d40sithui




msg:3878670
 6:35 pm on Mar 25, 2009 (gmt 0)

<your form will probably look something like this>

<form action="index.php" method="POST">
Item1<input type="text" name="item1"><br>
Item2<input type="text" name="item2"><br>
Item3<input type="text" name="item3"><br>
<input type="submit" value="submit">
</form>

<inside your action script>

$total = 0;
foreach(array_keys($_POST) as $key){

//assigns variables
$itemId = str_replace("item", "", $key); //takes off the "item" to reveal the item id
$amount = $_POST[$key];

//checks for invalid data - i.e not integer inputs

//query for price of item
$result = mysql_query("SELECT price FROM myItemsTable where id = ".$itemId);
$row = mysql_fetch_assoc($result);
$price = $row['price'];

//total price for this item
$subTotal = $price*$amount;

//grant total
$total+=$total;
}


Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved