Math - addition question with PHP & mySQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
RT
Forum Newbie
Posts: 5
Joined: Mon Apr 21, 2003 6:22 am
Location: Boston

Math - addition question with PHP & mySQL

Post by RT »

I'm new to PHP but learning quickly:

My question is I have 3 form values that people select. The values are numbers what I'm trying to do is add for a total the 3 values

Ex:

Plan1 =$249
Optional Services = $15

I need to create the ability to add the two together to create a Total = $264

Do I do this in the mySQL table or add the variables in the PHP code ?

If that's the case how do I do that and can someone point to a link that would explain how to do this ?

Thanks
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

PHP only, the mySQL-database, or databases in general, are usually only used to store data in, while PHP makes all the fancy stuff like calculations etc.

Code: Select all

<?php
$result=$_POST["Plan1"]+$_POST["Optional Services"];
echo "Result: ".$result;
?>
RT
Forum Newbie
Posts: 5
Joined: Mon Apr 21, 2003 6:22 am
Location: Boston

Thanks I'll try it

Post by RT »

Thanks for your input I'll give this a try -

One other question how do I insert the results into the Table ?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

connect to the database with something like this

$connection = @mysql_connect("localhost", "root", "");
$db = mysql_select_db("databasename", $connection) or die ("couldn't connect to database ".mysql_error());

Then use MySQL-statement "INSERT"

$str_sql="INSERT INTO $table (column1, column2,...) VALUES ($value1,$value2)";
$result = mysql_query($str_sql);

Might be worthwhile checking these tutorials - and (later on) what a database-wrapper is :

http://hotwired.lycos.com/webmonkey/pro ... rial4.html
http://www.devshed.com/Server_Side/MySQL/Intro/
http://www.freewebmasterhelp.com/tutorials/phpmysql/

Books on PHP/MySQL I found useful:

MySQL/PHP Database Applications (by Greenspan/Bulger)
PHP - Developer's Cookbook (by Hughes/Zmievski)

Also, always worthwhile checking out

http://www.php.net (the PHP-manual online)
and
http://www.mysql.com/documentation/index.html (the PHP-manual online)


[/url]
RT
Forum Newbie
Posts: 5
Joined: Mon Apr 21, 2003 6:22 am
Location: Boston

Tried it - didn't work

Post by RT »

Tried the code no luck - but I think part of the problem is I didn't explain myself correctly the first time.

I have a form that users select from. What I want to be able to do is have the total show at the botoom of the form - I'm using php to process the form and insert into tables.

<input name="total" type="text" id="total" value="<<?php
$total=$_POST["packplan"]+$_POST["optionservice"];
echo "total ".$result;
?>>


http://www.popwarnernet.net/testphp/order.php
Duke of PHP
Forum Newbie
Posts: 5
Joined: Mon Apr 21, 2003 2:20 pm

Much easier way of doing this

Post by Duke of PHP »

Seems to me like you are first starting with PHP. I think there is an easier way of approaching this. Let me get this straight...You are making an order form, and want to have the total calculated at the bottom?
RT
Forum Newbie
Posts: 5
Joined: Mon Apr 21, 2003 6:22 am
Location: Boston

I think you right

Post by RT »

In browsing around I think I actually have 2 separate issues here.

One is getting the form to do a calculation within itself.

The other is processing the information via php.

I actually have the php processing part done, its adding the fields in the html form that has me stumped right this second.

Years ago I seem to recall having to use Javascripting to do this but it's been awhile.

Ultimately what I'm trying to do is pass the total results on to Pay Pal for payment.

I suppose what I could do is put a continue order button at that bottom of the form and echo all the field input results to a new page with the total calculation done at that point and then insert the total into the table when they jump to Pay Pal.

Any help greatly appreciated.

Rob
Duke of PHP
Forum Newbie
Posts: 5
Joined: Mon Apr 21, 2003 2:20 pm

Try tackling it a different way.

Post by Duke of PHP »

Set up the form like this:

<code>

<?

echo "<form action=transaction.php method=post>
<b>Candy Bars:
<br>
QTY: <input type=text name=qty1 size=3>
<input type=submit value='Order'>
</form>

?>

</code>

Instead of using the dropdown menus like you were, or whatever, just use a quantity system. That would make things a lot easier too add together and also insert into the DB.
RT
Forum Newbie
Posts: 5
Joined: Mon Apr 21, 2003 6:22 am
Location: Boston

Reply

Post by RT »

actually when they select an option the value posted is a number what I'm trying to do is add two sets of selected numbers to at some point show the total value of the two options selected.

P.S. AIM = PopWNet if you want to do that as requested earlier.

RT
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Post by Jade »

I'd use javascript...its better at doing things on the fly than PHP is...

Jade
Post Reply