manuka Posted August 22, 2011 Share Posted August 22, 2011 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/245463-addition-issue/ Share on other sites More sharing options...
Pikachu2000 Posted August 22, 2011 Share Posted August 22, 2011 What errors are you getting? Quote Link to comment https://forums.phpfreaks.com/topic/245463-addition-issue/#findComment-1260723 Share on other sites More sharing options...
manuka Posted August 23, 2011 Author Share Posted August 23, 2011 According to Firefox's error consol there were a couple of errors with an attached css page but nothing that should affect this Quote Link to comment https://forums.phpfreaks.com/topic/245463-addition-issue/#findComment-1260724 Share on other sites More sharing options...
doddsey_65 Posted August 23, 2011 Share Posted August 23, 2011 ive never come across your method: $totalweight = ($info['count' * 'itemtype']); but im pretty sure thats not gonna work. Try $totalweight = $info['count'] * $info['itemtype']; Quote Link to comment https://forums.phpfreaks.com/topic/245463-addition-issue/#findComment-1260725 Share on other sites More sharing options...
bspace Posted August 23, 2011 Share Posted August 23, 2011 $totalweight = ($info['count' * 'itemtype']); Print $info['count'] . " "; Print $info['itemtype'] . " "; take a look at that calculation again Quote Link to comment https://forums.phpfreaks.com/topic/245463-addition-issue/#findComment-1260726 Share on other sites More sharing options...
manuka Posted August 23, 2011 Author Share Posted August 23, 2011 ive never come across your method: $totalweight = ($info['count' * 'itemtype']); but im pretty sure thats not gonna work. Try $totalweight = $info['count'] * $info['itemtype']; With that I get a return of "0" Quote Link to comment https://forums.phpfreaks.com/topic/245463-addition-issue/#findComment-1260728 Share on other sites More sharing options...
btherl Posted August 23, 2011 Share Posted August 23, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/245463-addition-issue/#findComment-1260729 Share on other sites More sharing options...
manuka Posted August 23, 2011 Author Share Posted August 23, 2011 Hi btherl, Yeah I can understand enough PHP to get why it doesn't work, however I don't quite know how to get it functioning how I need it to work.... Quote Link to comment https://forums.phpfreaks.com/topic/245463-addition-issue/#findComment-1260751 Share on other sites More sharing options...
doddsey_65 Posted August 23, 2011 Share Posted August 23, 2011 is 'itemType' the weight? have you tried typecasting? Not sure if it would help but its one of the debugging steps i would try $totalweight = ((int)$info['count'] * (int)$info['itemType']); Quote Link to comment https://forums.phpfreaks.com/topic/245463-addition-issue/#findComment-1260753 Share on other sites More sharing options...
btherl Posted August 23, 2011 Share Posted August 23, 2011 Can you add this code please: print_r($info); and paste the output. Quote Link to comment https://forums.phpfreaks.com/topic/245463-addition-issue/#findComment-1260754 Share on other sites More sharing options...
manuka Posted August 23, 2011 Author Share Posted August 23, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/245463-addition-issue/#findComment-1260762 Share on other sites More sharing options...
btherl Posted August 23, 2011 Share Posted August 23, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/245463-addition-issue/#findComment-1260793 Share on other sites More sharing options...
manuka Posted August 23, 2011 Author Share Posted August 23, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/245463-addition-issue/#findComment-1261073 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.