Jump to content

Adding foo onto bar (arithmetic)


Recommended Posts

I am trying to add foo onto bar inside a loop so I can then echo bar outside the loop and see the sum.

The PHP manual doesn't have anything on addition in the way I need it (adding onto), so I was wondering if anyone can help me?

 

LOL embrace yourself!

$query = "select * from products where id='{$content['id']}'"; $result = mysql_query($query); $row = mysql_fetch_array($result);
mysql_query ($query);
mysql_close($link);
echo "
	<B>Product ID:</B> {$content['id']}
	<BR>
	<B>Price:</B> $row[price] AUD
	<BR>
	<BR>
";
+$row[price] -> $total;
}
echo $total;

Link to comment
Share on other sites

It looks pretty, but sadly, looks dont count for much in code :)

Outside your loop, you need to create a variable, lets say $total;

the in your loop you can do it 2 ways.

$total += $row['price'];
// or //
$total = $total + $row['price'];

 

FYI: Arithmetic Operators

 

As an idea to save yourself some SQL queries, there is a wonderful MYSQL comparison function called IN()

You could write a nifty little script that will grab all of the results from one query, you can then loop through that.

Link to comment
Share on other sites

LOL oh God! The MySQL documentation is one manual I refuse to work with. It is designed so badly, to understand it, you must learn all of its dependency functions - it dumps a bunch of syntaxes at your feet - so if you haven't read the 500 pages before the page your on, you cannot understand it.

Thanks all the same about the MySQL manual, but I'd rather go out and buy another CPU and let there be more MySQL taps. Sadly the MySQL manual makes me have little passion for the language, despite I think it is awesome.

 

I'll study those two codes now and see which one I understand the best and has the best flow inside my brain and see what happens, and let you know the outcome. :)

Link to comment
Share on other sites

You are going to have to work with it sooner or later, if you want to learn the MySQL it is a must.

Otherwise your code could be constantly bogged down by poor optimization.

 

Getting a new CPU on a live server isnt really an option. Optimized code is.

Link to comment
Share on other sites

I've learn't pretty much all I need to with MySQL. 1.5 months I knew nothing of it, infact, I HATED it because it was too hard to learn because of its documentation... but I received ALLOT of help on another forum. I'm in the middle of writing my own MySQL manual which focusses on teaching new comers and speak in "street talk". In the official manual, after the tutorial everything is just in syntaxes with little to know street talk. This makes it incredibly difficult for noobs to find what there looking for as they don't know what syntax they need.

I don't want the next generation of new comer programmers to have to face what I did, so after my own project is finished I am going to polish my manual and pass on the teaching. Its gonna be great!

 

 

Ok, so I tried that code you suggested and I understand both operation flows.

I chose $total = $total + $row['price']; because that flows in the way I have always used the equals character.

The code didn't work. All I got was an echo of big round doughnut (a zero (0)).

 

$query = "select * from products where id='{$content['id']}'"; $result = mysql_query($query); $row = mysql_fetch_array($result);
mysql_query ($query);
mysql_close($link);
echo "
	<B>Product ID:</B> {$content['id']}
	<BR>
	<B>Price:</B> $row[price] AUD
	<BR>
	<BR>
";
$total = $total + $row['price'];
}
echo $total;

 

http://i54.tinypic.com/95wvtc.png

Note: In this session, I have added "id 1" four times. I was expecting $400. Also, if your wondering why you can see "Size" in the screen shot.. I stripped it from the code in this thread to keep it simple :)

Link to comment
Share on other sites

I've learn't pretty much all I need to with MySQL. 1.5 months I knew nothing of it, infact, I HATED it because it was too hard to learn because of its documentation... but I received ALLOT of help on another forum. I'm in the middle of writing my own MySQL manual which focusses on teaching new comers and speak in "street talk".

 

Street talk? That will be useless.

 

I don't want the next generation of new comer programmers to have to face what I did, so after my own project is finished I am going to polish my manual and pass on the teaching. Its gonna be great!

I've learn't pretty much all I need to with MySQL. 1.5 months I knew nothing of it, infact, I HATED it because it was too hard to learn because of its documentation... but I received ALLOT of help on another forum.

 

So, you know enough about mysql to rewrite the manual better then the developers who created it, work with it, and use it every day? You have got to be joking.

 

Sure, you could write a few tutorials on some simple select statement or whatever, but do you really think you know better than the people who currently write the manuals. Technical documentation is just that, it's not meant to be dumbed down because the technical details are often needed to understand the product. If you want to be a developer, you need to learn to enjoy reading technical reading material. If you don't enjoy doing so, you'll just plateaux out.

Link to comment
Share on other sites

Note: In this session, I have added "id 1" four times. I was expecting $400. Also, if your wondering why you can see "Size" in the screen shot.. I stripped it from the code in this thread to keep it simple :)

 

The code you posted doesn't even work.  You have the end of a loop that you didn't provide the start of the loop for.  You do a query, and then query again.  I can't account for the output you get, because we don't have the actual code you're using that is relevant to the problem. 

Link to comment
Share on other sites

The code you posted doesn't even work.  You have the end of a loop that you didn't provide the start of the loop for.  You do a query, and then query again.  I can't account for the output you get, because we don't have the actual code you're using that is relevant to the problem.

 

Thats true. I included that MySQL string because its relevant to the arithmetic code because its where $row['price'] comes from.

As the topic suggests, the thread is about the arithmetic part:

 

$total = $total + $row['price'];
}
echo $total;

Link to comment
Share on other sites

I went back to one of your other threads and had a look at your table.

You are storing price as a string with a $ in front of it.

You cannot add strings with arithmetic, as you see, it just doesnt work.

 

I would change your price column to a decimal(11,2) or something.

Link to comment
Share on other sites

I see!!! Your right:

http://i56.tinypic.com/ieed74.png

 

Thats OK, I just need to change a couple of pages to echo "$" infront of the coming number. Actually, this probably better for the future incase I need to perform arithmetic functions based on the price data.

I really have to have a shower LOL, so I'll have one and then report back the outcome! I'm pretty sure the solution you suggested is the reason why I've failed so far. :)

 

I'll be back soon. :)

Link to comment
Share on other sites

Hey, it summed up all good. Thanks for demonstrating how its done. I'm sure I'll be using this syntax some time in future.

 

I removed the dollar sign from MySQL and out of HTML on the 'products' page I have created.

 

When you say data types, do you mean like in MySQL? Such as VARCHAR? or are you refering to something in PHP.

 

PHP is so awesome! Every day I find out something new that can be done with it!!!!!! :D

Link to comment
Share on other sites

He's referring to the mysql data type.  It needs to match the type of data you are saving in that column.  His suggestion which seems good for handling currency is to use a decimal() type.  His example of DECIMAL(11,2) would allow you to store numbers as large as 999,999,999.99 in a column.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.