Jump to content

Addition Issue


manuka

Recommended Posts

Hi I'm having an issue with a piece of code, it pretty much works but the line to output the total weight only seems to be picking up the count and not multiplying it by the weight. Any ideas why this is?

 

Thanks in advance

 

<?php 

mysql_connect("localhost", "*****", "******") or die(mysql_error()); 

mysql_select_db("inventory") or die(mysql_error()); 

$data = mysql_query("SELECT * FROM testDB ORDER BY uid DESC LIMIT 30") 

or die(mysql_error()); 

Print "<p>"; 

while($info = mysql_fetch_array( $data )) 

{ 
$totalweight = ($info['count' * 'itemtype']);

Print $info['count'] . " "; 

Print $info['itemtype'] . " "; 

Print $totalweight . " kgs <br />"; 

} 

Print "</p>"; 

?> 

Link to comment
Share on other sites

To explain why it's not working, this is what php thinks:

 

$info['count' * 'itemtype']; # Hmm, multiplying two strings?  I'd better convert them to numbers first
= $info[0 * 0]; # Ok, that makes more sense.  Now simplify
= $info[0]; # Ok now we can look up item 0 in the array (which is indexed by name AND number)
= the count column, which happens to be index 0

Link to comment
Share on other sites

Can you add this code please:

 

print_r($info);

 

and paste the output.

 

Array ( [0] => 1 [count] => 1 [1] => bananas [itemtype] => bananas [2] => 5 [weight] => 5 [3] => 2011-08-23 [created] => 2011-08-23 [4] => 26 [uid] => 26 ) 10 fish kgs
Array ( [0] => 10 [count] => 10 [1] => fish [itemtype] => fish [2] => 10 [weight] => 10 [3] => 2011-08-23 [created] => 2011-08-23 [4] => 25 [uid] => 25 ) 10 weevil kgs
Array ( [0] => 10 [count] => 10 [1] => weevil [itemtype] => weevil [2] => 0 [weight] => 0 [3] => 2011-08-23 [created] => 2011-08-23 [4] => 24 [uid] => 24 ) 10 for kgs
Array ( [0] => 10 [count] => 10 [1] => for [itemtype] => for [2] => 2 [weight] => 2 [3] => 2011-08-23 [created] => 2011-08-23 [4] => 23 [uid] => 23 ) 12 goon kgs 

Link to comment
Share on other sites

Thanks.  I think what you want is this:

 

$totalweight = $info['count'] * $info['weight'];

 

Previously you were getting "0" because you were multiplying by $info['itemtype'], which is a string like "bananas".  PHP treats non-numeric strings as "0" in calculations.

 

Also to get a nicer output from print_r() you can do this:

 

print "<pre>"; print_r($info) ; print "</pre>";

 

The pre tags tell the browser to use text formatting instead of html formatting.

Link to comment
Share on other sites

Thanks.  I think what you want is this:

 

$totalweight = $info['count'] * $info['weight'];

 

Previously you were getting "0" because you were multiplying by $info['itemtype'], which is a string like "bananas".  PHP treats non-numeric strings as "0" in calculations.

 

Also to get a nicer output from print_r() you can do this:

 

print "<pre>"; print_r($info) ; print "</pre>";

 

The pre tags tell the browser to use text formatting instead of html formatting.

 

Thanks for that, basically I would have been on the right track a lot earlier if I had spotted that is was trying to multiply the wrong variable. Also cheers for the heads up on using the pre tags, that does make the formatting much more user friendly.

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.