how to make more than one variable out of multiple SQL info

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

User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

jramaro wrote:ahh i see ! ... thanks

exhausting the while loop, that's because i havent used while loops enough and so they're still unfamiliar , so i make them syntax and formatting mistakes.
Thanks for your help, I'm gonna go and see if i cant break it now :D

I need to add more records to make sure its adding them up

Thanks!
its not so much the while loop that was your issue, as it was the mysql_fetch_array. every time you call that (or mysql_fetch_row, or any other resource handler) it moves the pointer forward one resultset. therefore, you would need to make a second query after the first while loop to use on the second while loop... but thats just excess code and a waste of memory, so its just easier and better to do as much as possible in one loop.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

Can you post the code your using and what exatly you hope to achieve.
jramaro
Forum Commoner
Posts: 58
Joined: Tue Jun 26, 2007 7:46 am

Post by jramaro »

ah thanks :)

well that was pretty cool .
and I got everything working now and it all seems to be pretty good.

I had a slight problem but thats because i was shifting code around in a restructuring frenzy and forgot to put the long code in the
start and end { } curly brackets.

but i pieced it all back together and included what you showed me with what i had and now its all workin.

The only thing is that the Grand Total of the sum of al lthe $total_carts is just like 7791 for example.

its not doing the 7,791 .00

i tried a regular number_format($total_cart,2);

but that made it come out like = 7 (excluding al lthe rest of the numbers) dunno why .

You guys been a big help though , I appreciate it thanks! :D
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

the format not working is because you're adding the format to another format (both are strings) and php is converting them to an integer when you do the += on it.

Code: Select all

//instead of this
$totalSum+=number_format($total_cart, 2, '.', '');
echo $totalSum;

//do this
$totalSum+=$total_cart;
echo number_format($totalSum,2,'.','');
does that make sense?
Last edited by maliskoleather on Thu Sep 20, 2007 1:14 pm, edited 1 time in total.
jramaro
Forum Commoner
Posts: 58
Joined: Tue Jun 26, 2007 7:46 am

Post by jramaro »

yeah it makes sense, but if i dont want to echo it right then , can i make it into variable to use other places except that echo ?

is somethin like this ok?

Code: Select all

$totalSum=+$total_cart; 
$grand_total = number_format($totalSum,2,'.','');
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

yeah, that would work fine, assuming you dont try to add another integer to it :wink:
jramaro
Forum Commoner
Posts: 58
Joined: Tue Jun 26, 2007 7:46 am

Post by jramaro »

oh , thats only showing the last records total

is there any way to have it add them all up , but yet not have to echo it out like that?

and i noticed the way you posted:

Code: Select all

$totalSum=+$total_cart; 
echo number_format($totalSum,2,'.','');

Code: Select all

thats only echoing out the last instance of $total 
 not adding them together.
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

errr.. typo. += not =+
jramaro
Forum Commoner
Posts: 58
Joined: Tue Jun 26, 2007 7:46 am

Post by jramaro »

lol , oh
yeah that works but its essentially the same thing as the other code now that i look closer at it.

They're both doing the same thing. they format the decimal point but they dont include a comma.

so example number is like (with dollar sign) : $ 3875.50

the two codes from before and the one you just gave are pretty much the same i think.

here they are

Code: Select all

$grand_total = $totalSum+=number_format($total_cart, 2, '.', ''); 

$totalSum+=$total_cart; 
$grand_total = number_format($totalSum,2,'.','');


they're both semeing identical and both outputting the same way.

I can live with it as long as the decimal is there
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

technically their different, but it has to do with the data types.

essentially the output the same stuff... its just better practice to only format it right before you echo the string.

as for the comma, thats the last parameter in the number_format() function.

Code: Select all

//with comma
echo number_format($totalSum,2,'.',',');

//without
echo number_format($totalSum,2,'.','');
jramaro
Forum Commoner
Posts: 58
Joined: Tue Jun 26, 2007 7:46 am

Post by jramaro »

cool thank you. :)

I knew of number_format()
but not of the manual formating .

It's working now. and number example: $ 3,972.00

Thanks for all the help today

:)
Post Reply