Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding column data - chopped values 1

Status
Not open for further replies.

dle46163

IS-IT--Management
Jul 9, 2004
81
US
This is probably an easy question but I seem to be missing something here. I have a column in a MySql database called Unit_Price. While I read in the row data for screen display I'm also totaling the Unit_Price field to display a grand total at the bottom. I'm using this command inside my WHILE statement:

$unit_total += $Unit_Price['sum'];

Everything works fine as long as I have single digit prices like 2.00, 3.00, etc.. If I have something larger like 50.00, 500.00, or larger, only the first digit (the 5) is being read and added to my total. What am I missing?
 
How is 'Unit_Price' defined in MySQL? Where is the array '$Unit_Price' coming from?

Without knowing some of what your program looks like, it's hard to tell you what's wrong or missing.

Ken
 
$Unit_Price is coming from a database query which is looking up info in a column called Unit_Price. I issue the query, then use this while statement to add up all the results of Unit_Price which the total I dump into $unit_total.

while ($row_result = mysql_fetch_array($result_result))
{
extract($row_result);
$unit_total += $Unit_Price['sum'];
}

This works fine except when I have a number larger than 1 place to the left of my decimal. Only thing I can think is it's something to do with the database format. I've got the field set for 'decimal', length of 8 and 2 decimal places. Any thoughts?


 
It's not clear why you're referencing '$Unit_Price' as an array in your code.

Instead of what you're doing, try this:
Code:
while ($row_result = mysql_fetch_assoc($result_result))
   $unit_total += $row_result['Unit_Price'];
In my code $row_result is an associative array where the keys are your column names.

If this doesn't give you what you're after, I suggest you do the following to see what is being returned:
Code:
while ($row_result = mysql_fetch_assoc($result_result)) {
   echo '<pre>';print_r($row_result);echo '</pre>',"\n"; }

Ken
 
That worked great!!! Thank you very much!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top