php - 产品价格在 MYSQL 数据库中以 PHP 显示为 69 英镑,而不是 69.00 英镑

标签 php mysql

这里有一个奇怪的问题,当循环从数据库中提取的产品时,某些产品价格显示为完整的整数。例如 £69,但有些正确显示为 £69.00。我不知道为什么有些产品显示正确,而另一些则不正确。

我的数据库中的价格列是小数(10,2),所有产品的所有值都包含 2 位小数。

我将向您展示在前端显示数据所采取的步骤,以便为您提供尽可能多的详细信息,同时省略相关详细信息。

$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;
}

使用数据,我只需执行如下所示的 foreach:

<?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">&pound;<?= $product['price'] ?></span>
            <!-- More goes here but again, its not relevant to my problem -->                  
            </article>
        </div>
        <?php
    }
?>

现在当我 print_r(pageContent) 到数组时看起来像这样:

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
                )

        )
)

有人有什么想法吗?

最佳答案

使用 number_format() 函数获取价格。 Click在这里查看功能

<span class="green-header">&pound;<?= number_format($product['price'],2) ?></span>

关于php - 产品价格在 MYSQL 数据库中以 PHP 显示为 69 英镑,而不是 69.00 英镑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37432162/

相关文章:

php - 将 codeigniter 与 php 和 mysql 一起使用时,在此服务器上找不到请求的 URL 错误

php - 通过覆盖现有数据来管理MySQL表中的数据

php - 使用 Guzzle 在 header 中获取参数 'Link'

mysql - PHP MySQL,获取下一个 key

mysql - 为什么 SQL 不匹配两个字符串?

mysql - SQL select query - 从不同字段中选择

php - 哪个唯一 ID 用于博客和评论?

java - 只有第一个 ArrayList<String> 有效

php - npm run开发和npm run生产之间的区别

PHP 比较数据库中的值