How to store the following JSON result into HTML table? - php

{
"total_count":3,
"offset":2,
"limit":2,
"notifications":
[
{
"id":"481a2734-6b7d-11e4-a6ea-4b53294fa671",
"successful":15,
"failed":1,
"converted":3,
"remaining":0,
"queued_at":1415914655,
"send_after":1415914655,
"canceled": false,
"url": "https://yourWebsiteToOpen.com",
"data":null,
"headings":{
"en":"English and default langauge heading",
"es":"Spanish language heading"
},
"contents":{
"en":"English and default content",
"es":"Hola"
}
},
{
"id":"b6b326a8-40aa-13e5-b91b-bf8bc3fa26f7",
"successful":5,
"failed":2,
"converted":0,
"remaining":0,
"queued_at":1415915123,
"send_after":1415915123,
"canceled": false,
"url": nil,
"data":{
"foo":"bar",
"your":"custom metadata"
},
"headings":{
"en":"English and default langauge heading",
"es":"Spanish language heading"
},
"contents":{
"en":"English and default content",
"es":"Hola"
}
}
]
}

Assuming you are wanting to display your JSON data within a HTML table, here is a basic example:
<?php
$json=<<<JSON
[
{
"name": "foo",
"age": 23,
"mood": "happy"
},
{
"name": "bar",
"age": 38,
"mood": "sad"
}
]
JSON;
$people = json_decode($json);
?>
<html>
<table>
<thead>
<tr><th>Name</th><th>Age</th><th>Mood</th></tr>
</thead>
<tbody>
<?php foreach($people as $person): ?>
<tr>
<td><?= $person->name; ?></td>
<td><?= $person->age; ?></td>
<td><?= $person->mood; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</html>

You can easily do it by using AppML, here is a tutorial which will get you started quickly.

Related

PHP Populate Table Headers Based on Data from Database

I have a table_headers database table that contains the id, header text, parent_id, and order_number that contains this data.
How do I loop to this record in such a way that it makes this kind of HTML?
<tr>
<th colspan='2'>Header 1</th>
<th colspan='6'>Header 2</th>
<th rowspan='3'>Header 3</th>
</tr>
<tr>
<th rowspan=2>H1 Child 1</th>
<th rowspan=2>H1 Child 2</th>
<th rowspan=2>h2 Child 1</th>
<th colspan=4>h2 Child 2</th>
</tr>
<tr>
<th>H2 C2 C1</th>
<th>H2 C2 C2</th>
<th>H2 C2 C3</th>
<th>H2 C2 C4</th>
</tr>
That should result to this.
Lastly, how to have an array that would contain the IDs of the lowest child just like this.
$lowest_child[0]=4; //because the ID of `H1 Child 1` is 4
$lowest_child[1]=5; //because the ID of `H1 Child 2` is 5 and so on
$lowest_child[2]=;
$lowest_child[3]=;
$lowest_child[4]=;
$lowest_child[5]=;
$lowest_child[6]=;
$lowest_child[7]=;
Another Example Data Set using the answer provided by ggordon.
It outputs to this. It seems that the order number of children is not followed.
Solution
This can be achieved after generating some helpful metadata. The code below traverses and builds a tree in an associative array. With the assistance of some helper functions getRowSpan and getColSpan the table is generated from the metadata. The json metadata generated has been included below.
In order to get the array of the lowest children. You simply need to use $levels[$depth] which will return an associative array of children at the lowest depth. The keys are the ids of the children.
HTML/PHP Code
<!DOCTYPE html>
<html>
<body>
<?php
$headings = []; # store heading data as array
$numOfRows = count($headings); # 3
$levels = [];
$depth = 0;
$subHeadings = [];
$levels[$depth]=[];
for($i=0;$i<$numOfRows;$i++){
if($headings[$i]["parent_id"]==null){
$headings[$i]["child_count"]=0;
$headings[$i]["child_ids"]=[];
$headings[$i]["depth"]=0;
$levels[$depth][$headings[$i]["id"]]=$headings[$i];
}else{
$subHeadings[]=$headings[$i];
}
}
while(count($subHeadings)>0){
$depth++;
$levels[$depth]=[];
$subHeadingNext=[];
foreach($subHeadings as $sub){
if(
isset( $levels[$depth-1][
$sub["parent_id"]
])
){
$levels[$depth-1][$sub["parent_id"]]["child_count"]++;
$levels[$depth-1][$sub["parent_id"]]["child_ids"][]=$sub["id"];
$sub["child_count"]=0;
$sub["child_ids"]=[];
$sub["depth"]=$depth;
$levels[$depth][$sub["id"]]=$sub;
}else{
$subHeadingNext[] = $sub;
}
}
$subHeadings=$subHeadingNext;
}
#echo json_encode($levels,true);
#colspan is total of all child nodes
function getColSpan($node,$levels){
$childCount = $node["child_count"];
$visitor = $node;
$childrenIds = $visitor["child_ids"];
foreach($childrenIds as $childId){
$childCount+= getColspan($levels[$visitor["depth"]+1][$childId],$levels);
}
return $childCount;
}
#max depth - depth of last child
function getRowSpan($node,$maxDepth){
return $node["child_count"] > 0 ? 1 : $maxDepth - $node["depth"]+1;
}
?>
<table border="3">
<?php
foreach($levels as $level => $nodes){
?>
<tr>
<?php
foreach($nodes as $node){
?>
<td colspan="<?php echo getColSpan($node,$levels) ?>" rowspan="<?php echo getRowSpan($node,$depth) ?>"><?php echo $node["header"] ?></td>
<?php
}
?>
</tr>
<?php
}
?>
</table>
</body>
</html>
Output
<table border="3">
<tbody><tr>
<td colspan="2" rowspan="1">Header 1</td>
<td colspan="6" rowspan="1">Header 2</td>
<td colspan="0" rowspan="3">Header 3</td>
</tr>
<tr>
<td colspan="0" rowspan="2">H1 Child 1</td>
<td colspan="0" rowspan="2">H1 Child 2</td>
<td colspan="0" rowspan="2">H2 Child 1</td>
<td colspan="4" rowspan="1">H2 Child 2</td>
</tr>
<tr>
<td colspan="0" rowspan="1">H2-C2-C1</td>
<td colspan="0" rowspan="1">H2-C2-C2</td>
<td colspan="0" rowspan="1">H2-C2-C3</td>
<td colspan="0" rowspan="1">H2-C2-C4</td>
</tr>
</tbody></table>
JSON Meta Data Generated
This is the processed result stored in $levels. It is an associated array. Each level starting from 0 can be accessed using $levels[0]. Within each level there is another associative array with nodes/tr/table cells/headings . Each heading node is indexed by the id i.e. to get Heading 1 we would use $levels[0][1]. You may see the additional metadata (depth, child ids and children count) generated and stored below:
[
{
"1":{
"id":"1",
"header":"Header 1",
"parent_id":null,
"order_number":"1",
"child_count":2,
"child_ids":[
"4",
"5"
],
"depth":0
},
"2":{
"id":"2",
"header":"Header 2",
"parent_id":null,
"order_number":"1",
"child_count":2,
"child_ids":[
"6",
"7"
],
"depth":0
},
"3":{
"id":"3",
"header":"Header 3",
"parent_id":null,
"order_number":"1",
"child_count":0,
"child_ids":[
],
"depth":0
}
},
{
"4":{
"id":"4",
"header":"H1 Child 1",
"parent_id":"1",
"order_number":"1",
"child_count":0,
"child_ids":[
],
"depth":1
},
"5":{
"id":"5",
"header":"H1 Child 2",
"parent_id":"1",
"order_number":"1",
"child_count":0,
"child_ids":[
],
"depth":1
},
"6":{
"id":"6",
"header":"H2 Child 1",
"parent_id":"2",
"order_number":"1",
"child_count":0,
"child_ids":[
],
"depth":1
},
"7":{
"id":"7",
"header":"H2 Child 2",
"parent_id":"2",
"order_number":"1",
"child_count":4,
"child_ids":[
"12",
"13",
"14",
"15"
],
"depth":1
}
},
{
"12":{
"id":"12",
"header":"H2-C2-C1",
"parent_id":"7",
"order_number":"1",
"child_count":0,
"child_ids":[
],
"depth":2
},
"13":{
"id":"13",
"header":"H2-C2-C2",
"parent_id":"7",
"order_number":"2",
"child_count":0,
"child_ids":[
],
"depth":2
},
"14":{
"id":"14",
"header":"H2-C2-C3",
"parent_id":"7",
"order_number":"3",
"child_count":0,
"child_ids":[
],
"depth":2
},
"15":{
"id":"15",
"header":"H2-C2-C4",
"parent_id":"7",
"order_number":"4",
"child_count":0,
"child_ids":[
],
"depth":2
}
}
]
Update 1: Get Nodes With No Children
The following function traverses levels to extract the nodes with no children
function getNodesWithNoChildren($levels){
$nodesWithNoChildren = [];
foreach($levels as $level => $nodes){
foreach($nodes as $node){
if($node['child_count']==0){
$nodesWithNoChildren[]=$node;
}
}
}
return $nodesWithNoChildren;
}
$nodeNamesWithNoChildren = array_map(function($node){
return $node["header"];
}, getNodesWithNoChildren($levels));
echo implode(", ",$nodeNamesWithNoChildren);
Output
Header 3, H1 Child 1, H1 Child 2, H2 Child 1, H2-C2-C1, H2-C2-C2, H2-C2-C3, H2-C2-C4
Update 2: Get Nodes With No Children (Left To Right)
function getLeftToRightNodesWithoutChildren($startNode,$levels){
$nodesWithNoChildren = [];
if($startNode == null){
foreach($levels[0] as $headerNode){
$subNodes = getLeftToRightNodesWithoutChildren($headerNode,$levels);
foreach($subNodes as $node){
$nodesWithNoChildren[]=$node;
}
}
}else{
if($startNode['child_count']==0){
$nodesWithNoChildren[]=$startNode;
}
foreach($startNode['child_ids'] as $childId){
$childNode = $levels[$startNode["depth"]+1][$childId];
$subNodes = getLeftToRightNodesWithoutChildren($childNode,$levels);
foreach($subNodes as $node){
$nodesWithNoChildren[]=$node;
}
}
}
return $nodesWithNoChildren;
}
$nodeNamesWithNoChildren = array_map(function($node){
return $node["header"];
}, getLeftToRightNodesWithoutChildren(null,$levels));
echo implode(", ",$nodeNamesWithNoChildren);
Output
H1 Child 1, H1 Child 2, H2 Child 1, H2-C2-C1, H2-C2-C2, H2-C2-C3, H2-C2-C4, Header 3
Getting "array that would contain the IDs of the lowest child"
$lowest_child = array_map(function($node){
return $node["id"];
}, getLeftToRightNodesWithoutChildren(null,$levels));
echo implode(", ",$lowest_child);
Output
4, 5, 6, 12, 13, 14, 15, 3

How to Convert HTML Table to JSON in PHP

I am trying to convert some html into an array then into a json string.
I am developing based on this reference: https://www.codeproject.com/Tips/1074174/Simple-Way-to-Convert-HTML-Table-Data-into-PHP-Arr
This is the basic table/html from which I want to convert it to JSON.
<table class="table-list table table-responsive table-striped">
<thead>
<tr>
<th class="coll-1 name">name</th>
<th class="coll-2">height</th>
<th class="coll-3">weight</th>
<th class="coll-date">date</th>
<th class="coll-4"><span class="info">info</span></th>
<th class="coll-5">country</th>
</tr>
</thead>
<tbody>
<tr>
<td class="coll-1 name">
<i class="flaticon-user"></i>
Jhon Doe
</td>
<td class="coll-2 height">45</td>
<td class="coll-3 weight">50</td>
<td class="coll-date">9am May. 16th</td>
<td class="coll-4 size mob-info">abcd</td>
<td class="coll-5 country">CA</td>
</tr>
<tr>
<td class="coll-1 name">
<i class="flaticon-user"></i>
Kasim Shk
</td>
<td class="coll-2 height">33</td>
<td class="coll-3 weight">54</td>
<td class="coll-date">Mar. 14th '18</td>
<td class="coll-4 size mob-info">ijkl</td>
<td class="coll-5 country">UAE</td>
</tr>
</tbody>
</table>
I want json output like this:
[
{
"user_link": "/username/Jhon Doe/",
"name": "Jhon Doe",
"height": "45",
"weight": "50",
"date": "Apr. 01st '18",
"info": "abcd",
"country": "CA"
},
{
"user_link": "/username/Kasim Shk/",
"name": "Kasim Shk",
"height": "33",
"weight": "54",
"date": "Mar. 14th '18",
"info": "ijkl",
"country": "UAE"
}
]
This is what I was trying in PHP :(
However, this is not able to fetch user's link and the name is not proper in JSON.
(HTML Table same as above # http://rudraproduction.in/htmltable.php)
header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json');
error_reporting(E_ERROR | E_PARSE);
$time_start = microtime(true);
$All = [];
$link = 'http://rudraproduction.in/htmltable.php';
$jsonData = file_get_contents($link);
//echo $jsonData;
$dom = new DOMDocument;
$dom->loadHTML($jsonData);
$tables = $dom->getElementsByTagName('table');
$tr = $dom->getElementsByTagName('tr');
foreach ($tr as $element1) {
for ($i = 0; $i < count($element1); $i++) {
//Not able to fetch the user's link :(
$link = $element1->getElementsByTagName('td')->item(0)->getElementsByTagName('a'); // To fetch user link
$name = $element1->getElementsByTagName('td')->item(0)->textContent; // To fetch name
$height = $element1->getElementsByTagName('td')->item(1)->textContent; // To fetch height
$weight = $element1->getElementsByTagName('td')->item(2)->textContent; // To fetch weight
$date = $element1->getElementsByTagName('td')->item(3)->textContent; // To fetch date
$info = $element1->getElementsByTagName('td')->item(4)->textContent; // To fetch info
$country = $element1->getElementsByTagName('td')->item(5)->textContent; // To fetch country
array_push($All, array(
"user_link" => $link,
"name" => $name,
"height" => $height,
"weight" => $weight,
"date" => $date,
"info" => $info,
"country" => $country
));
}
}
echo json_encode($All, JSON_PRETTY_PRINT);
Output of my PHP code is:
[
{
"user_link": "http:\/\/rudraproduction.in\/htmltable.php",
"name": null,
"height": null,
"weight": null,
"date": null,
"info": null,
"country": null
},
{
"user_link": "http:\/\/rudraproduction.in\/htmltable.php",
"name": "\r\n\t\t\r\n\t\tJhon Doe\r\n\t",
"height": "45",
"weight": "50",
"date": "9am May. 16th",
"info": "abcd",
"country": "CA"
},
{
"user_link": "http:\/\/rudraproduction.in\/htmltable.php",
"name": "\r\n\t\t\r\n\t\tKasim Shk\r\n\t",
"height": "33",
"weight": "54",
"date": "Mar. 14th '18",
"info": "ijkl",
"country": "UAE"
}
]
I prefer to use XPath with DomDocument because of utility/ease of the syntax. By targeting the only the <tr> elements inside the <tbody> tag, you can access all required data.
With the exception of the href value, the final "all-letters" substring in each <td> class value represents your desired key for the associated value. For this I am using preg_match() to extract the final "word" in the class attribute.
When the $key is name, the href attribute value must be stored with the hardcode key: user_link.
Your sample date values require some preparation to yield the desired format. As your input data varies, you may need to modify the regular expression to allow strtotime() to properly handle the date expression.
Code: (Demo)
$html = <<<HTML
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<table class="table-list table table-responsive table-striped" border="1">
<thead>
<tr>
<th class="coll-1 name">name</th>
<th class="coll-2">height</th>
<th class="coll-3">weight</th>
<th class="coll-date">date</th>
<th class="coll-4"><span class="info">info</span></th>
<th class="coll-5">country</th>
</tr>
</thead>
<tbody>
<tr>
<td class="coll-1 name">
<i class="flaticon-user"></i>
Jhon Doe
</td>
<td class="coll-2 height">45</td>
<td class="coll-3 weight">50</td>
<td class="coll-date">9am May. 16th</td>
<td class="coll-4 size mob-info">abcd</td>
<td class="coll-5 country">CA</td>
</tr>
<tr>
<td class="coll-1 name">
<i class="flaticon-user"></i>
Kasim Shk
</td>
<td class="coll-2 height">33</td>
<td class="coll-3 weight">54</td>
<td class="coll-date">Mar. 14th '18</td>
<td class="coll-4 size mob-info">ijkl</td>
<td class="coll-5 country">UAE</td>
</tr>
</tbody>
</table>
</body>
</html>
HTML;
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
foreach ($xpath->query('//tbody/tr') as $tr) {
$tmp = []; // reset the temporary array so previous entries are removed
foreach ($xpath->query("td[#class]", $tr) as $td) {
$key = preg_match('~[a-z]+$~', $td->getAttribute('class'), $out) ? $out[0] : 'no_class';
if ($key === "name") {
$tmp['user_link'] = $xpath->query("a[#class = 'icon']", $td)[0]->getAttribute('href');
}
$tmp[$key] = trim($td->textContent);
}
$tmp['date'] = date("M. dS 'y", strtotime(preg_replace('~\.|\d+[ap]m *~', '', $tmp['date'])));
$result[] = $tmp;
}
var_export($result);
echo "\n----\n";
echo json_encode($result);
Output: (as multidim array, then json encoded string)
array (
0 =>
array (
'user_link' => '/username/Jhon Doe/',
'name' => 'Jhon Doe',
'height' => '45',
'weight' => '50',
'date' => 'May. 16th \'18',
'info' => 'abcd',
'country' => 'CA',
),
1 =>
array (
'user_link' => '/username/Kasim Shk/',
'name' => 'Kasim Shk',
'height' => '33',
'weight' => '54',
'date' => 'Jan. 01st \'70',
'info' => 'ijkl',
'country' => 'UAE',
),
)
----
[{"user_link":"\/username\/Jhon Doe\/","name":"Jhon Doe","height":"45","weight":"50","date":"May. 16th '18","info":"abcd","country":"CA"},{"user_link":"\/username\/Kasim Shk\/","name":"Kasim Shk","height":"33","weight":"54","date":"Jan. 01st '70","info":"ijkl","country":"UAE"}]

multidimensional array in angular have issue

What is the issue in my code?
return array is
{"records":
[{"Status":"1",
"Date":"2017-07-14 10:46:33",
"Email":"cy#gmail.com","Company":"Inc.",
"Model":"Model 8081 A","Animation":"Walk, Turn Around","id":"1",
"Note":"This is a new request for model with animation.",
"Attachment":
"[{'url':'request/31a.jpg','name':'a.jpg'},{'url':'request/42Light.png','name':'Light.png'}]"
}]
}
And HTML code is
<tr ng-repeat="x in records">
<td>{{x.Status}}</td>
<td>{{x.Date}}</td>
<td>{{x.Email}}</td>
<td>{{x.Company}}</td>
<td>{{x.Model}}</td>
<td>{{x.Animation}}</td>
<td>{{x.Note}}</td>
<td>
<table>
<tr ng-repeat="lnk in x.Attachment">
<td>{{lnk.url}}</td>
<td>{{lnk.name}}</td>
</tr>
</table>
</td>
</tr>
lnk.url and lnk.name print nothing.
Error in console is [ngRepeat:dupes]
Your attachment is not an array, it is a string. Your return array should be like this:
{
"records": [{
"Status": "1",
"Date": "2017-07-14 10:46:33",
"Email": "cy#gmail.com",
"Company": "Inc.",
"Model": "Model 8081 A",
"Animation": "Walk, Turn Around",
"id": "1",
"Note": "This is a new request for model with animation.",
"Attachment": [{
"url": "request/31a.jpg",
"name": "a.jpg"
}, {
"url": "request/42Light.png",
"name": "Light.png"
}]
}]
}
(Notice removed quotes in Attachment).
So you should convert Attachment with JSON.parse() function in your controller.
Attachment is a string not an array. convert it to an array and it will l work
angular.module("app",[])
.controller("ctrl",function($scope){
$scope.records = [
{
"Status":"1",
"Date":"2017-07-14 10:46:33",
"Email":"cy#gmail.com",
"Company":"Inc.",
"Model":"Model 8081 A",
"Animation":"Walk, Turn Around",
"id":"1",
"Note":"This is a new request for model with animation.",
"Attachment":[{'url':'request/31a.jpg','name':'a.jpg'},{'url':'request/42Light.png','name':'Light.png'}]
}
]
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<table>
<tr ng-repeat="x in records">
<td>{{x.Status}}</td>
<td>{{x.Date}}</td>
<td>{{x.Email}}</td>
<td>{{x.Company}}</td>
<td>{{x.Model}}</td>
<td>{{x.Animation}}</td>
<td>{{x.Note}}</td>
<td>
<table>
<tr ng-repeat="lnk in x.Attachment">
<td>{{lnk.url}}</td>
<td>{{lnk.name}}</td>
</tr>
</table>
</td>
</tr>
</table>
</div>

How can i parse json to a html table using PHP?

I have json file like this
[{
"Shop_name": "916",
"Shop_id": "916TCR",
"Address":"cdsasffafa"
"numbers": "4",
"mob_no": "9447722856"
}, {
"Shop_name": "Chicking",
"Shop_id": "CKGTCR",
"Address":"afagagg",
"numbers": "8",
"mob_no": "6767564532"
}]
i want to see this as HTML Table with row head as Shop_name,Shop_id,Address
and Data in next row.
i tried decoding it and display as table (How can i parse json into a html table using PHP?).
but Json File format is diffrent here.
Also like to know if Json File gets bigger with [0],[1],[2]....
You should parse your json data to php object than you should iterate your data like below;
$myData = <<<JSON
[
{"Shop_name":"minh",
"Shop_id":"916TCR",
"Address":"cdsasffafa",
"numbers":"4",
"mob_no":"9447722856"
},
{"Shop_name":"Chig",
"Shop_id":"CKGTCR",
"Address":"afagagg",
"numbers":"8",
"mob_no":"6767564532"
}
]
JSON;
$myObject = json_decode($myData);
?>
<table>
<tr>
<td>Shop Name</td>
<td>Shop ID</td>
<td>Address</td>
<td>Numbers</td>
<td>Mob No</td>
</tr>
<?PHP
foreach($myObject as $key=>$item)
{
?>
<tr>
<td><?PHP echo $item->Shop_name; ?></td>
<td><?PHP echo $item->Shop_id; ?></td>
<td><?PHP echo $item->Address; ?></td>
<td><?PHP echo $item->numbers; ?></td>
<td><?PHP echo $item->mob_no; ?></td>
</tr>
<?PHP
}
?>
</table>
A working example is here: http://ideone.com/IqZLMs
PS: You should fix this ” quote to "

Using "+=" operator in PHP

I have a foreach loop that I'm running through JSON data. One of the values in the JSON is a number:
{
"apiVersion": "0.1",
"data": {
"offset": 0,
"limit": 50,
"count": 50,
"total": 783,
"reports": [
{
"type": "ROOM_CHARGE",
"data": {
"datetime": "2014-11-10T11:08:08-08:00",
"originator": "265a",
"roomNumber": "265",
"chargeItem": "Premium Services",
"chargeAmountCents": 495
}
},
{
"type": "STB_EVENT_TIMED",
"data": {
"datetime": "2014-11-10T10:38:20-08:00",
"originator": "242b",
"roomNumber": "242",
"eventItem": "Watched movie 31008",
"duration": "P0Y0M0DT0H7M50.827S"
}
},
{
"type": "ROOM_CHARGE",
"data": {
"datetime": "2014-11-10T09:35:37-08:00",
"originator": "540a",
"roomNumber": "540",
"chargeItem": "Premium Services",
"chargeAmountCents": 495
}
},
You'll notice above the "ROOM_CHARGE" refers to "chargeAmountCents": 495. I'd like to run my foreach loop and add every number together as such:
Notice below my code with $total. I basically want to add all the room charges together, and then echo out the total at the end. I can format the number myself, I'm just having trouble with the "+=" operator and not sure if it is supposed to be used this way? I keep getting "495" when I echo $total.
<?php foreach($output1['data']['reports'] as $reporting): ?>
<?php if($reporting['type'] == "ROOM_CHARGE") : ?>
<tr>
<td><?php echo 'Room Charge'; ?></td>
<td><?php echo substr($reporting['data']['datetime'], -26, 10) ?></td>
<td><?php echo substr($reporting['data']['datetime'], -14, 8) ?></td>
<td><?php echo ($reporting['data']['roomNumber']) ?> </td>
<td><?php echo ($reporting['data']['originator']) ?> </td>
<td><?php echo ($reporting['data']['chargeItem']) ?></td>
<td><?php echo number_format(($reporting['data']['chargeAmountCents'])/100, 2, '.', ',') ?></td>
<td>-</td>
<?php $total = ""; ?>
<?php $total += ($reporting['data']['chargeAmountCents']) ?>
</tr>
Inside your loop, you are doing <?php $total = ""; ?>. So, every time you loop, you are resetting the total.
You need to set $total (to 0, since it's a number) outside the loop.
<?php $total = 0; ?>
<?php foreach($output1['data']['reports'] as $reporting): ?>
<?php if($reporting['type'] == "ROOM_CHARGE") : ?>
<tr>
<td><?php echo 'Room Charge'; ?></td>
<td><?php echo substr($reporting['data']['datetime'], -26, 10) ?></td>
<td><?php echo substr($reporting['data']['datetime'], -14, 8) ?></td>
<td><?php echo ($reporting['data']['roomNumber']) ?> </td>
<td><?php echo ($reporting['data']['originator']) ?> </td>
<td><?php echo ($reporting['data']['chargeItem']) ?></td>
<td><?php echo number_format(($reporting['data']['chargeAmountCents'])/100, 2, '.', ',') ?></td>
<td>-</td>
<?php $total += ($reporting['data']['chargeAmountCents']) ?>
</tr>

Categories