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);