I have a database where I need to store the price of items. Now when I get the values from the database, I would like commas added for the thousand value e.g convert 19000 to 19,000.
<?php echo $row['pprice'];?>
How can I change the format?
You can try using this.
echo number_format($row['pprice']);
Not sure if its correct but wont hurt to try and see if it works.
Or create a new variable for the $row['pprice'] so like this.
$commaPrice = $row['pprice'];
echo number_format($commaPrice);
I suggest to use money_format:
$number = 1234.56;
setlocale(LC_MONETARY, 'it_IT.UTF8');
echo money_format('%i', $number) . "\n";
Note that you need to specify your current locale. If you want to use different locales than yours, you must build them install them on the operating system.
$newNumber = number_format($number);
Related
I need to remove the beginning part of a URL:
$test = "price/edit.php";
echo ltrim($test,'price/');
shows dit.php
Here is a codepad if you want to fiddle: https://codepad.remoteinterview.io/DominantCalmingBerlinPrice
Any ideas what is going on? I want it to echo edit.php of course.
ltrim removes ALL characters found, consider the following:
$test = 'price/edit.php';
echo ltrim($test, 'dprice/'); // outputs t.php
For this particular scenario, you should probably be using str_replace.
The second argument to ltrim() is a character mask (a list of characters) that should be removed. e is a character that should be removed and so it is removed from edit.
There are many string manipulations that you could use, however since this is a filename/filepath the correct tool is a Filesystem Function, basename():
echo basename($test);
For more information on the filepath check into pathinfo().
Hi I have faced the same problem some time ago and found this solution use it if it suits your need
<?php
$test = "price/edit.php";
echo ltrim(ltrim($test,'price'),'/');
output
edit.php
but i must say you should use basename as all type problem
<?php
$test = "project/price/edit.php";
// echo ltrim(ltrim($test,'price'),'/');// this will give oject/price/edit.phpedit.php
echo basename($test); // and it will generate edit.php
To display our prices correctly in a slider on Magento I use this line:
<?php $first_amount_before_split = number_format($this->helper('tax')->getPrice($_product, $_product->getFinalPrice()), '2', '.', ','); $my_array = explode(".", $first_amount_before_split);?>
And I echo that line with: <span class="main-price"><?php echo $my_array[0]; ?>,-</span>
But prices above 1000 are displayed as: 1,100,- and I want to display this as 1.100,-
So I would like to change the symbol , for a .
How can I achieve that?
You want to check out number format to accomplish this:
http://php.net/manual/en/function.number-format.php
Assuming this is a localization related thing you are trying to achieve, you should probably use Magento’s built-in localization methods for displaying prices.
I have a URL that contains a department name that will pull records from the database.
The URL looks like this: submissions.php?department=Settings,%20Security%20&%20Payments which is the equivalent of Settings, Security & Payments
My query needs to pull records from the table where the department is equal to Settings, Security & Payments.
How can convert that GET variable back to what I would expect it to be?
I tried html_entity_decode but it ignores the & and only gave me everything prior to that.
Whats the best way to do that?
Side note, if it was my data I would make it simple and pull it by ID but we dont have a table that has ID's for the departments.
Try urldecode()
You can see the manual here. http://uk3.php.net/urldecode
<?php
$string = "submissions.php?department=Settings,%20Security%20&%20Payments";
$decoded = urldecode($string);
echo "Original string: $string\n";
echo "Decoded string: $decoded\n";
?>
http://codepad.org/Bq1Gt30s
Use urldecode($your_URLstring)
this is a problem I keep running into, and would like to know the best way to do this.
I'm trying to split each value in this array into an individual string,
$domainext = array("com","net","edu","uk","au","in","biz","ca","cc","cd","bz","by");
Here is how I'm doing it.
foreach ($domainext as $ext){
$ext = implode(', ', $domainext);
echo $ext."<br>";
echo "<br>";
}
this is the output.
com, net, edu, uk, au, in, biz, ca, cc, cd, bz, by
(however, there are as many lines as there are array values)
I have tried using explode, and it returns "array" with an error above it.
Any help would be greatly appreciated, and sorry for such a basic question.
I think this is what you are looking for:
$domainext = array("com","net","edu","uk","au","in","biz","ca","cc","cd","bz","by");
print implode("\n", $domainext);
This gives:
com
net
edu
....
Oops... replace the "\n" by a br tag if you are printing to a web page.
print implode("<br/>", $domainext);
You dont need to implode or explode anything :), try this:
foreach ($domainext as $ext){
echo $ext."<br>";
echo "<br>";
}
If you just want to output the items in your array on a different row, you cna use join
echo join("<br/>",$domainext);
As others have said, no loop necessary.
Simply:
echo join('<br>', $domainext);
will do the job if you want each domain suffix separated with HTML line breaks (the exact required output is unclear from your question). Join is an alias of implode, and preferable from its clearer meaning.
I have date in this format: 2010-01-11. I want to display JAN-11. How can i do this using php?
<?php echo date("M-y",strtotime("2010-01-11"));?>
Rudu's answer is correct, +1.
"M-y" would result to "Jan-11"
so if you need the output to be UPPERCASE, dont forget to use strtoupper().
<?php echo strtoupper(date("M-y",strtotime("2010-01-11")));?>
also bookmark this for later reference.
http://www.php.net/manual/en/function.date.php
Cheers