从MYSQL数据库中,产品价格在PHP中显示为69英镑而不是69.00英镑
Strange problem here, when looping through products pulled from the database, some product prices are showing as a full int. For example £69, but some are showing correctly as £69.00. I don't know why some of the products are showing correct and others are not.
The price column in my database is decimal(10,2) and all values for all products contain 2 decimal points.
I will show you the steps that I take to displaying the data on the front-end to give you as much detail as possible while leaving out relevant details.
$pageContent = getHomeContent($conn);
function getHomeContent($conn)
{
$topProducts = getTopProducts($conn);
//I do more here but nothing relevant to my problem
$homeContent = [
'topProducts' => $topProducts
];
return $homeContent;
}
function getTopProducts($conn)
{
$companyId = companyId(); //returns an integer
$products = [];
$sql = "SELECT products.id, products.name,products.description, products.feature_image , products.price, products.slug,products.special_offer,products.special_offer_start , category.slug as category_slug
FROM products
JOIN category_product ON products.id = category_product.product_id
JOIN category ON category.id = category_product.category_id WHERE products.company_id = ". $companyId ."
ORDER BY products.amount_sold DESC LIMIT 3";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$products[] = $row;
}
}
return $products;
}
With the data I simply do a foreach look like this:
<?php
foreach($pageContent['topProducts'] as $product){
?>
<div class="col-xs-12 col-sm-4">
<article class="product">
<img src="<?= $product['feature_image'] ?>" class="img-responsive"/>
<span class="green-header">£<?= $product['price'] ?></span>
<!-- More goes here but again, its not relevant to my problem -->
</article>
</div>
<?php
}
?>
Now when I print_r(pageContent) to array looks like this:
Array
(
[topProducts] => Array
(
[0] => Array
(
[id] => 136
[name] => product name
[description] => product description
[feature_image] => product url
[price] => 69
[slug] => product-slug
[special_offer] => 0
[special_offer_start] => 2013-03-19
[category_slug] => product slug
)
[1] => Array
(
[id] => 136
[name] => product name
[description] => product description
[feature_image] => product url
[price] => 69.00
[slug] => product-slug
[special_offer] => 0
[special_offer_start] => 2013-03-19
[category_slug] => product slug
)
[2] => Array
(
[id] => 136
[name] => product name
[description] => product description
[feature_image] => product url
[price] => 78.00
[slug] => product-slug
[special_offer] => 0
[special_offer_start] => 2013-03-19
[category_slug] => product slug
)
)
)
Does anyone have any ideas?
Use number_format()
function for the price.Click here to check the function
<span class="green-header">£<?= number_format($product['price'],2) ?></span>
Regardless of the what comes in from the Database, you can always use the PHP's Built number Format. That's the the way to go in this scenario. The Reason is that when PHP sees the value: 69.00 it automatically casts it to an int since it doesn't have any meaningful Fractional Part. So to get your Fraction back just use the number format like so:
<?php
$price = number_format($product['price'],2);
<span class="green-header">£<?php echo price; ?></span>