duanshan3065 2019-07-19 16:43
浏览 57

在创建JSON时在PHP中使用money_function()

The following PHP script looks at data from mySQL table and creates string array in JSON.

The two values presented in the array are sumTotal and numvalue.

I would like to format both of these values as currency with the proper decimal placement.

I know that the money_format() function can be implemented somehow but I am having trouble understanding how this could be done.

The following is what I have so far:

if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql =  "SELECT name, SUM(value) as numvalue 
         FROM Table1
            LEFT JOIN Table2 USING(DevName)
         WHERE name <> '' 
         and name is not null
         GROUP BY name";

if ($result = mysqli_query($con, $sql)) {

    $resultArray = array();
    $tempArray = array();

    // you want an array of objects, so create an object to sum the sub totals
    $total = new stdClass;
    $total->sumTotal = 0;

    while($row = $result->fetch_object()) {
        $tempArray = $row;
        array_push($resultArray, $tempArray);

        $total->sumTotal = bcadd($total->sumTotal, $row->numvalue);
    }

    $resultArray[] = $total;
    echo json_encode($resultArray);
}

mysqli_close($con);
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等
    • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
    • ¥15 qt6.6.3 基于百度云的语音识别 不会改
    • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
    • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
    • ¥20 测距传感器数据手册i2c