vet911 Posted June 14, 2012 Share Posted June 14, 2012 I'm trying to get a percentage of a price from a table column. All I get for values are zeros. what I need is $360.00 * 10% = 36, from the code i'm trying to figure out I get zero. If i change $commission = $row['price']; to this I get the value of the row price from the database. Any help would be appreciated. Please! <?php include 'config.php'; mysql_connect($dbhost, $dbuser, $dbpass)or die("cannot connect to store database"); mysql_select_db("xxxxxx")or die("cannot select DB"); $query = "SELECT cut_stones.itemno, cut_stones.sold, cut_stones.price, commission.paid FROM cut_stones LEFT JOIN commission ON cut_stones.itemno = commission.itemno WHERE sold = '1' ORDER BY itemno"; $result = mysql_query($query) or die("There was a problem with the SQL query: " . mysql_error()); //$rows = mysql_num_rows($result); ?> <center><div><table border='1' width='300'> <tr><th align="center" width="15%">Item No.</th><th align="center" width="10%">Sold</th><th align="center" width="10%">Price</th><th align="center" width="10%">Paid</th><th align="center" width="10%">Commission</th></tr> <?php while($row = mysql_fetch_array( $result )) { $percent = 10; echo $percent; echo "<br/>"; $commission = ($row['price'] * $percent); echo $commission; ?> <tr><td width="25%"><?= $row['itemno']; ?></td><td align="center" width="10%"><?= $row['sold']; ?></td><td align="center" width="10%"><?= $row['price']; ?></td><td align="center" width="10%"><?= $row['paid']; ?></td><td align="center" width="10%"><?= $commission ?></td></tr> <?php } ?> </table></div></center> <?php mysql_close(); ?> <br> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/264204-im-having-trouble-with-math-and-percentages/ Share on other sites More sharing options...
Mahngiel Posted June 14, 2012 Share Posted June 14, 2012 Since percentages are based off of 100, 10 percent equates to 10. 360 / 10 = 36 100 / 10 = 10 If all you want is 10% of something, it's right there. If you want to add 10% 360 * 1.10 Subtract 10% 360 - (360 / 10) Quote Link to comment https://forums.phpfreaks.com/topic/264204-im-having-trouble-with-math-and-percentages/#findComment-1353996 Share on other sites More sharing options...
insidus Posted June 15, 2012 Share Posted June 15, 2012 The % in most programming languages isn't a percentage, instead, its modulus/remainder $a % $b Modulus Remainder of $a divided by $b. So to work out percentage, its what the OP said Quote Link to comment https://forums.phpfreaks.com/topic/264204-im-having-trouble-with-math-and-percentages/#findComment-1354010 Share on other sites More sharing options...
vet911 Posted June 15, 2012 Author Share Posted June 15, 2012 I understand that but what I'm getting is zero not what I'm looking for. So if what your saying I should get a number 10 times my $row['price']. How can I fix this? $commission = ($row['price'] * $percent); echo $commission; Quote Link to comment https://forums.phpfreaks.com/topic/264204-im-having-trouble-with-math-and-percentages/#findComment-1354025 Share on other sites More sharing options...
insidus Posted June 15, 2012 Share Posted June 15, 2012 to get 1% of a number, in a normal maths sum, you would do Number / 100 this gives you 1%, then * 10 for 10%, or *50 for 50%. so you would do $percent = 10; $commission = (($row['price'] /100) * $percent); echo "You get: " . $commission . " (10%)"; Quote Link to comment https://forums.phpfreaks.com/topic/264204-im-having-trouble-with-math-and-percentages/#findComment-1354031 Share on other sites More sharing options...
Mahngiel Posted June 15, 2012 Share Posted June 15, 2012 Insidus is correct, but making it more difficult than it needs to be. 100% represents the full value of a number X. Therefore 100% is mathematically 1:1 As there are 10 tens in 100, 10% can be justified as X / 10 Furthermore, there are 100 ones, so 1% can be justified as X / 100 <?php // Commission is 1% (100 / 1 = 100) $commission = $row['price'] / 100; // Commission is 10% (100 / 10 = 10) $commision = $row['price'] / 10; // Commission is 25% (100 / 25 = 4) $commission = $row['price'] / 4; /// et cetera ad nauseum If you want to round down you can use floor(), if you want to round to two decimal spaces, you can use number_format Quote Link to comment https://forums.phpfreaks.com/topic/264204-im-having-trouble-with-math-and-percentages/#findComment-1354033 Share on other sites More sharing options...
insidus Posted June 15, 2012 Share Posted June 15, 2012 That works if your funding a percent off 100. But say if you wanted 40 percent of 60. 60/40 = 1.5. But I'd you get 1% first by 60/100 = 0.6, then * 40 = 24. By working out 1%, you can times it to find any percentage Quote Link to comment https://forums.phpfreaks.com/topic/264204-im-having-trouble-with-math-and-percentages/#findComment-1354091 Share on other sites More sharing options...
vet911 Posted June 15, 2012 Author Share Posted June 15, 2012 Thanks for the help, as it turns out I had the Dollar sign in the column for the price ($120.00) and it was preventing the calculation, I changed it to (120.00) now it works. Quote Link to comment https://forums.phpfreaks.com/topic/264204-im-having-trouble-with-math-and-percentages/#findComment-1354102 Share on other sites More sharing options...
Mahngiel Posted June 15, 2012 Share Posted June 15, 2012 That works if your funding a percent off 100. But say if you wanted 40 percent of 60. 60/40 = 1.5. But I'd you get 1% first by 60/100 = 0.6, then * 40 = 24. By working out 1%, you can times it to find any percentage 100 / 40 = 2.5. 60 / 2.5 = 24 Quote Link to comment https://forums.phpfreaks.com/topic/264204-im-having-trouble-with-math-and-percentages/#findComment-1354117 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.