Jump to content

I'm having trouble with math and percentages.


vet911

Recommended Posts

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>

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%)";

 

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.