dougang1965 2016-05-25 08:52
浏览 26
已采纳

从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">&pound;<?= $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?

  • 写回答

2条回答 默认 最新

  • doujiao9574 2016-05-25 08:58
    关注

    Use number_format() function for the price.Click here to check the function

    <span class="green-header">&pound;<?= number_format($product['price'],2) ?></span>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?