PHP Mysqli add subtract number format - php

HI just found a solution about add and sustract two columns here but I need to make the php code to format the numbers.
Here is the solution I got here:
<?php
while ($row= mysqli_fetch_assoc($result)) {
echo $row['money'] - $row['cost'];
}
?>
Its working fine for what I need but here is the thing. I use to run the follow php code to all money and numbers
<?php echo number_format($product['cost'],0, ",", "."); ?>
That way the numbers will display without decimals and like this 100.000
How can I make the code I found here to work with number format?
Something like this I tried but does not work
<?php
while ($row= mysqli_fetch_assoc($result)) {
echo number_format$row['money'] - number_format$row['cost'];
}
?>
It must substract and then show the result with formated numbers

you need to put it in ()
echo number_format($row['money'] - $row['cost'],0,',','.');
More than 2 :
echo number_format($row['money'] - $row['cost'] + $row['whatever'] * $row['whatever'],0,',','.');

Related

Number_format PDO results

Hey guys so I'm just wondering how it is possible to format numbers when display data from a MySQL table using PDO?
At the moment I am printing my data using the following code:
<?php while($row = $results->fetch(PDO::FETCH_ASSOC))
{
echo '<div>
'.$row["Price"].'
</div>
';
} ?>
I have tried implementing number_format into my PHP however I keep getting a parse error.
Any idea how I can implement it properly so that my numbers contain commas?
Thanks
while($row = $results->fetch(PDO::FETCH_ASSOC))
{
echo '<div>
'.number_format($row['price'], 2, ',', '.').'
</div>
';
}
Should do the trick.
Maybe you tried without the last parameter of number format, the function takes one, two or four parameters.

PHP sorting 5 numbers per row

Hi I have made script that shows only even numbers and now i have to do that shows only 5 even numbers per row example:
numbers must be sorted like this
2,4,6,8,10
12,14,16,18,20
i must have numbers written like that current code that i am using for showing only even numbers is under. but if someone can help me how can i show only 5 per row i will be thankful. Thanks in advance.
<?php
$p=100;
for($p=100;$p>=0;)
{
echo "$p,";
$p=$p-2;
}
?>
Try something like this
<?PHP
$p=100;
$count=0;
for($p=100;$p>=0;)
{
echo "$p";
$p=$p-2;
$count+=1;
if($count==5){
$count=0;
echo "<br/>";
}
else echo " ,";
}
?>
One-liner:
echo implode("\n",array_map(function($a) {return implode(",",$a);},array_chunk(range(0,100,2),5)));
Result:
0,2,4,6,8
10,12,14,16,18
20,22,24,26,28
30,32,34,36,38
40,42,44,46,48
50,52,54,56,58
60,62,64,66,68
70,72,74,76,78
80,82,84,86,88
90,92,94,96,98
100
Function reference: implode, array_map, array_chunk, range.
For rendering in a browser, replace \n with <br />.
try the following
<?php for($p=100;$p>=0;$p-2){
echo "$p";
if($p%5==0){
echo "<br>";
}
else {
echo ", ";
}
}
?>

Putting Something Inside a Link with PHP

So I'm basically calling and returning an entire row from a mysql table using a while loop (which is working), but I'm trying to use the data that I call inside an html link, but I can't seem to get it to work.
Ideally, eventually it will just be a list of links with each person's individual name. I can return the list fine, but I can't seem to return the list with a link.
Here is my code that I feel should be working :(
<?php
require 'db/connect.php';
$result = $con->query('SELECT distinct name FROM mytable');
while($rows = $result->fetch_assoc())
{
echo ''$rows['name']'' , "</br>";
}
?>
Any help would be greatly appreciated!
Issue might be with your string concatenation. Try following code block
echo ''.$rows['name'].'';
echo ''. $rows['name']. '' , "</br>";
You just need to use . to concatenate strings together.
try this
echo ''.$rows['name'].'' , "</br>";
Should work just fine. Basically it's '.$row['name'].'
when concatenating strings with variables you have to use dot(.) like echo "string".$var; it will be invalid to write echo "string"$var; in your example you have ignored this point.

PHP echo adding functions

What I am trying to do is get an echo of the following php call and subtract 14.1% from the displayed number.
The code:
<?php echo $program->current_amount(); ?>
Can I add arithmetic functions to this in order to display the 14.1% deduction?
I think you're looking for a basic math operation in your output that has no effect on a database or anything else, correct?
If so, do something like the following:
<?php
// Set values
$current_amount = 100;
$pcnt_off = 14.1;
// Do the math
$out = $current_amount - ($pcnt_off/100) * $current_amount;
// Output
echo $out . " is " . $pcnt_off . "% off of " . $current_amount;
?>
http://codepad.org/RqF8cuvN
More specifically to your case:
<?php echo $program->current_amount() - 0.141 * $program->current_amount(); ?>
You can perform expressions inside an echo statement, yes; just wrap it in a (), so:
<?php echo ($program->current_amount() - .141); ?>
It may not even be necessary to use (). Incidentally, if your environment supports short tags, you can simply do:
<?= $program->current_amount() - .141 ?>
Keep in mind, though, that that code won't actually remove 14.1% from your number--you would want to multiply by .859.

Number format in PHP

I want to display price in my front page. But the format of the number required is,
Entered price: 10000 Display: 10,000
Entered price: 10000.10 Display: 10,000.1
Entered price: 10000.01 Display: 10,000.01
if I am using the following code
echo number_format ($price,2,'.',',');
But through this the result is displayed in this manner.
Entered price: 10000 Display: 10,000.00
Please help me to solve this problem
There's a function in PHP called money_format().
Have a look at it on http://php.net/manual/en/function.money-format.php
You have set number of decimial points to 2 so that is why you have 10,000.00. Try to user in this way:
echo number_format ($price,1,'.',',');
And also it is better to use money_format if you are working with money values.
Surely, from a clarity and consistency point of view, having 2 digits after the decimal point makes more sense especially when showing prices.
#barryhunter made a valid point and the following doesn't work.
echo rtrim(number_format($price,2,'.',','),'0.');
However, this does:
trim(trim(number_format($price,2,'.',','),'0'),'.');
Look:
<?php
$a=array('10000.00','10000.10','10000.01');
foreach ($a as $price)
{
echo $price.' - '.rtrim(rtrim(number_format($price,2,'.',','),'0'),'.')."\n";
}
?>
$> php -f t.php
10000.00 - 10,000
10000.10 - 10,000.1
10000.01 - 10,000.01
Personally I would do
echo number_format($price,floor($price)==$price?0:2,'.',',');
displaying a price as 10,000.1 just looks odd to me.
But if you really must
$bits = explode('.',$price);
echo number_format($price,strlen($bits[1]),'.',',');
(edit) In reply to the comment, it works for me...
<?php
$a=array(10000.00,10000.10,10000.01);
foreach ($a as $price)
{
$bits = explode('.',$price);
echo $price.' - '.number_format($price,strlen($bits[1]),'.',',')."\n";
}
?>
$ php t.php
10000 - 10,000
10000.1 - 10,000.1
10000.01 - 10,000.01

Categories