Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Programming Q&A
Creating a Table From Specific PHP Array Keys
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="brsy" data-source="post: 312218" data-attributes="member: 2912"><p>I have a PHP array with ~10 keys, each having their own values. There are ~5 entries of each of the ~10 keys, meaning I have ~50 keys in total. How can I create a table like this:</p><p><img src="http://criesval.tk/img.png" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p></p><p>How can I create this using PHP? Here's what I have now, which just puts all the information into a table. So basically, how do I dissect it into Dates and ItemDescriptions? Here's the code that I'm currently using.</p><p>[SPOILER="Le Code"][PHP]<?php</p><p></p><p>$jSON = '{</p><p> "LabelNumber":"oa0021148274",</p><p> "Scans":[</p><p> {</p><p> "Type":"P",</p><p> "Courier":"020",</p><p> "Description":"Picked up",</p><p> "Date":"06/11/2014 18:58:14",</p><p> "Name":"Cape Town",</p><p> "Franchise":"CPT",</p><p> "Status":"PPP",</p><p> "StatusDescription":"Your parcel was picked up",</p><p> "CompanyInfo":{</p><p> "contactName":"",</p><p> "company":"",</p><p> "address1":"",</p><p> "address2":"",</p><p> "address3":"",</p><p> "address4":"",</p><p> "address5":"",</p><p> "address6":"",</p><p> "address7":"",</p><p> "address8":"",</p><p> "comment":""</p><p> },</p><p> "UploadDate":"07/11/2014",</p><p> "Signature":""</p><p> },</p><p> {</p><p> "Type":"T",</p><p> "Courier":"020",</p><p> "Description":"Pretoria",</p><p> "Date":"06/11/2014 19:04:30",</p><p> "Name":"Cape Town",</p><p> "Franchise":"CPT",</p><p> "Status":"PRY",</p><p> "StatusDescription":"Your parcel is currently in transit between our Cape Town and Pretoria depots.",</p><p> "CompanyInfo":{</p><p> "contactName":"",</p><p> "company":"",</p><p> "address1":"",</p><p> "address2":"",</p><p> "address3":"",</p><p> "address4":"",</p><p> "address5":"",</p><p> "address6":"",</p><p> "address7":"",</p><p> "address8":"",</p><p> "comment":""</p><p> },</p><p> "UploadDate":"07/11/2014",</p><p> "Signature":""</p><p> },</p><p> {</p><p> "Type":"T",</p><p> "Courier":"011",</p><p> "Description":"Pretoria Depot Scan ",</p><p> "Date":"10/11/2014 06:56:34",</p><p> "Name":"Pretoria",</p><p> "Franchise":"PRY",</p><p> "Status":"O01",</p><p> "StatusDescription":"Your parcel has been received in the Pretoria depot.",</p><p> "CompanyInfo":{</p><p> "contactName":"",</p><p> "company":"",</p><p> "address1":"",</p><p> "address2":"",</p><p> "address3":"",</p><p> "address4":"",</p><p> "address5":"",</p><p> "address6":"",</p><p> "address7":"",</p><p> "address8":"",</p><p> "comment":""</p><p> },</p><p> "UploadDate":"10/11/2014",</p><p> "Signature":""</p><p> },</p><p> {</p><p> "Type":"T",</p><p> "Courier":"013",</p><p> "Description":"Onboard",</p><p> "Date":"10/11/2014 07:51:19",</p><p> "Name":"Pretoria",</p><p> "Franchise":"PRY",</p><p> "Status":"ONB",</p><p> "StatusDescription":"Onboard - the parcel is onboard the Courier vehicle.",</p><p> "CompanyInfo":{</p><p> "contactName":"",</p><p> "company":"Home Delivery",</p><p> "address1":"44 suikerbos",</p><p> "address2":"lydiana",</p><p> "address3":"o",</p><p> "address4":"",</p><p> "address5":"",</p><p> "address6":"",</p><p> "address7":"",</p><p> "address8":"",</p><p> "comment":""</p><p> },</p><p> "UploadDate":"10/11/2014",</p><p> "Signature":""</p><p> },</p><p> {</p><p> "Type":"D",</p><p> "Courier":"013",</p><p> "Description":"Signature Obtained",</p><p> "Date":"10/11/2014 09:47:05",</p><p> "Name":"Pretoria",</p><p> "Franchise":"PRY",</p><p> "Status":"YES",</p><p> "StatusDescription":"Signature Obtained - Your parcel has been delivered and signed for.",</p><p> "CompanyInfo":{</p><p> "contactName":"philimon",</p><p> "company":"Home Delivery",</p><p> "address1":"44 suikerbos",</p><p> "address2":"lydiana",</p><p> "address3":"o",</p><p> "address4":"",</p><p> "address5":"",</p><p> "address6":"",</p><p> "address7":"",</p><p> "address8":"",</p><p> "comment":""</p><p> },</p><p> "UploadDate":"10/11/2014",</p><p> "Signature":"http://api.fastway.org/signatures/0312408478f58b8b1d685a148089960f.jpg"</p><p> }</p><p> ],</p><p> "Signature":"http://api.fastway.org/signatures/0312408478f58b8b1d685a148089960f.jpg",</p><p> "DistributedTo":"Cape Town ",</p><p> "DistributedDate":"8/08/2014",</p><p> "Reference":"",</p><p> "IsOnforward":"0"</p><p>}';</p><p></p><p>$trackingInfo = json_decode($jSON, true);</p><p></p><p>foreach($trackingInfo['Scans'] as $key => $value)</p><p>{</p><p> </p><p> echo "<table>";</p><p> foreach ($value as $key2 => $value2)</p><p> {</p><p> echo "<tr><td>". $key2 ."</td>";</p><p> echo "<td>". $value2 ."</td></tr>";</p><p> }</p><p> echo "</table>";</p><p></p><p> echo ")";</p><p>}</p><p>?>[/PHP][/SPOILER]</p><p>[DOUBLEPOST=1416266785,1416266017][/DOUBLEPOST]<strong>UPDATE</strong>.</p><p></p><p>I managed to figure out how to get individual values, but I have to now figure out how to make the table.</p><p></p><p>[PHP]foreach($trackingInfo['Scans'] as $key => $value) {</p><p> $count = count($trackingInfo['Scans']);</p><p> $dates[$count] = $value['Date'] . "<br>";</p><p> print_r($dates);</p><p>}[/PHP]</p><p>[DOUBLEPOST=1416267598][/DOUBLEPOST]<strong>UPDATE:</strong></p><p>I fixed it.</p><p>[PHP]$getLabel = htmlspecialchars($_GET['label']);</p><p>$jSON = file_get_contents('http://api.fastway.org/v2/tracktrace/detail/'. $getLabel .'?api_key=7e54d8c9fc8ca8ea2e5ea057eb85c4c6');</p><p>/*$jSON = '{</p><p> "result":{</p><p> "LabelNumber":"oa0021148274",</p><p> "Scans":[</p><p> {</p><p> "Type":"P",</p><p> "Courier":"020",</p><p> "Description":"Picked up",</p><p> "Date":"06/11/2014 18:58:14",</p><p> "Name":"Cape Town",</p><p> "Franchise":"CPT",</p><p> "Status":"PPP",</p><p> "StatusDescription":"Your parcel was picked up",</p><p> "CompanyInfo":{</p><p> "contactName":"",</p><p> "company":"",</p><p> "address1":"",</p><p> "address2":"",</p><p> "address3":"",</p><p> "address4":"",</p><p> "address5":"",</p><p> "address6":"",</p><p> "address7":"",</p><p> "address8":"",</p><p> "comment":""</p><p> },</p><p> "UploadDate":"07/11/2014",</p><p> "Signature":""</p><p> },</p><p> {</p><p> "Type":"T",</p><p> "Courier":"020",</p><p> "Description":"Pretoria",</p><p> "Date":"06/11/2014 19:04:30",</p><p> "Name":"Cape Town",</p><p> "Franchise":"CPT",</p><p> "Status":"PRY",</p><p> "StatusDescription":"Your parcel is currently in transit between our Cape Town and Pretoria depots.",</p><p> "CompanyInfo":{</p><p> "contactName":"",</p><p> "company":"",</p><p> "address1":"",</p><p> "address2":"",</p><p> "address3":"",</p><p> "address4":"",</p><p> "address5":"",</p><p> "address6":"",</p><p> "address7":"",</p><p> "address8":"",</p><p> "comment":""</p><p> },</p><p> "UploadDate":"07/11/2014",</p><p> "Signature":""</p><p> },</p><p> {</p><p> "Type":"T",</p><p> "Courier":"011",</p><p> "Description":"Pretoria Depot Scan ",</p><p> "Date":"10/11/2014 06:56:34",</p><p> "Name":"Pretoria",</p><p> "Franchise":"PRY",</p><p> "Status":"O01",</p><p> "StatusDescription":"Your parcel has been received in the Pretoria depot.",</p><p> "CompanyInfo":{</p><p> "contactName":"",</p><p> "company":"",</p><p> "address1":"",</p><p> "address2":"",</p><p> "address3":"",</p><p> "address4":"",</p><p> "address5":"",</p><p> "address6":"",</p><p> "address7":"",</p><p> "address8":"",</p><p> "comment":""</p><p> },</p><p> "UploadDate":"10/11/2014",</p><p> "Signature":""</p><p> },</p><p> {</p><p> "Type":"T",</p><p> "Courier":"013",</p><p> "Description":"Onboard",</p><p> "Date":"10/11/2014 07:51:19",</p><p> "Name":"Pretoria",</p><p> "Franchise":"PRY",</p><p> "Status":"ONB",</p><p> "StatusDescription":"Onboard - the parcel is onboard the Courier vehicle.",</p><p> "CompanyInfo":{</p><p> "contactName":"",</p><p> "company":"Home Delivery",</p><p> "address1":"44 suikerbos",</p><p> "address2":"lydiana",</p><p> "address3":"o",</p><p> "address4":"",</p><p> "address5":"",</p><p> "address6":"",</p><p> "address7":"",</p><p> "address8":"",</p><p> "comment":""</p><p> },</p><p> "UploadDate":"10/11/2014",</p><p> "Signature":""</p><p> },</p><p> {</p><p> "Type":"D",</p><p> "Courier":"013",</p><p> "Description":"Signature Obtained",</p><p> "Date":"10/11/2014 09:47:05",</p><p> "Name":"Pretoria",</p><p> "Franchise":"PRY",</p><p> "Status":"YES",</p><p> "StatusDescription":"Signature Obtained - Your parcel has been delivered and signed for.",</p><p> "CompanyInfo":{</p><p> "contactName":"philimon",</p><p> "company":"Home Delivery",</p><p> "address1":"44 suikerbos",</p><p> "address2":"lydiana",</p><p> "address3":"o",</p><p> "address4":"",</p><p> "address5":"",</p><p> "address6":"",</p><p> "address7":"",</p><p> "address8":"",</p><p> "comment":""</p><p> },</p><p> "UploadDate":"10/11/2014",</p><p> "Signature":"http://api.fastway.org/signatures/0312408478f58b8b1d685a148089960f.jpg"</p><p> }</p><p> ],</p><p> "Signature":"http://api.fastway.org/signatures/0312408478f58b8b1d685a148089960f.jpg",</p><p> "DistributedTo":"Cape Town",</p><p> "DistributedDate":"8/08/2014",</p><p> "Reference":"",</p><p> "IsOnforward":"0"</p><p> },</p><p> "generated_in":"187ms"</p><p>}';*/</p><p></p><p></p><p>@$trackingInfo = json_decode($jSON, true);</p><p>if(strpos($jSON, 'does not have any scans') === false) {</p><p> if(isset($jSON) && !empty($jSON)) {</p><p> echo "</p><p> <style></p><p> td {</p><p> padding: 7px 15px 7px 15px;</p><p> }</p><p> tr:nth-child(even) {</p><p> background-color: #ccc;</p><p> }</p><p> tr:nth-child(odd) {</p><p> background-color: #fff;</p><p> }</p><p> </style></p><p> ";</p><p> </p><p> echo "<h2>Tracking Number:<span style='font-weight:normal'> ". $trackingInfo['result']['LabelNumber'] ."</span></h2>";</p><p> echo "<table style='border: 1px solid #ccc;'>";</p><p> echo "<tr><td><h4>Date: </h4></td>";</p><p> echo "<td><h4>Status: </h4></tr>";</p><p> </p><p> foreach($trackingInfo['result']['Scans'] as $key => $value) {</p><p> $count = count($trackingInfo['Scans']);</p><p> </p><p> $dates[$count] = $value['Date'];</p><p> $desc[$count] = $value['StatusDescription'];</p><p> $signature[$count] = $value['Signature'];</p><p> </p><p> echo "<tr><td>". $dates[$count] ."</td>";</p><p> echo "<td>". $desc[$count] ."</td></tr>";</p><p> </p><p> if(!empty($signature[$count])) {</p><p> $siggy = $signature[$count];</p><p> echo "<tr><td>Signature</td><td><img src='". $siggy ."' alt='signature' /></td></tr>";</p><p> }</p><p> }</p><p> echo "</table>";</p><p>} else {</p><p> echo "<h2>POST data not found. Please resubmit the form.</h2>";</p><p> return false;</p><p> }</p><p>} else {</p><p> echo "<h2>The label you have used does not exist. If you believe this is an error, please contact us.</h2>";</p><p> return false;</p><p>}</p><p>[/PHP]</p></blockquote><p></p>
[QUOTE="brsy, post: 312218, member: 2912"] I have a PHP array with ~10 keys, each having their own values. There are ~5 entries of each of the ~10 keys, meaning I have ~50 keys in total. How can I create a table like this: [IMG]http://criesval.tk/img.png[/IMG] How can I create this using PHP? Here's what I have now, which just puts all the information into a table. So basically, how do I dissect it into Dates and ItemDescriptions? Here's the code that I'm currently using. [SPOILER="Le Code"][PHP]<?php $jSON = '{ "LabelNumber":"oa0021148274", "Scans":[ { "Type":"P", "Courier":"020", "Description":"Picked up", "Date":"06/11/2014 18:58:14", "Name":"Cape Town", "Franchise":"CPT", "Status":"PPP", "StatusDescription":"Your parcel was picked up", "CompanyInfo":{ "contactName":"", "company":"", "address1":"", "address2":"", "address3":"", "address4":"", "address5":"", "address6":"", "address7":"", "address8":"", "comment":"" }, "UploadDate":"07/11/2014", "Signature":"" }, { "Type":"T", "Courier":"020", "Description":"Pretoria", "Date":"06/11/2014 19:04:30", "Name":"Cape Town", "Franchise":"CPT", "Status":"PRY", "StatusDescription":"Your parcel is currently in transit between our Cape Town and Pretoria depots.", "CompanyInfo":{ "contactName":"", "company":"", "address1":"", "address2":"", "address3":"", "address4":"", "address5":"", "address6":"", "address7":"", "address8":"", "comment":"" }, "UploadDate":"07/11/2014", "Signature":"" }, { "Type":"T", "Courier":"011", "Description":"Pretoria Depot Scan ", "Date":"10/11/2014 06:56:34", "Name":"Pretoria", "Franchise":"PRY", "Status":"O01", "StatusDescription":"Your parcel has been received in the Pretoria depot.", "CompanyInfo":{ "contactName":"", "company":"", "address1":"", "address2":"", "address3":"", "address4":"", "address5":"", "address6":"", "address7":"", "address8":"", "comment":"" }, "UploadDate":"10/11/2014", "Signature":"" }, { "Type":"T", "Courier":"013", "Description":"Onboard", "Date":"10/11/2014 07:51:19", "Name":"Pretoria", "Franchise":"PRY", "Status":"ONB", "StatusDescription":"Onboard - the parcel is onboard the Courier vehicle.", "CompanyInfo":{ "contactName":"", "company":"Home Delivery", "address1":"44 suikerbos", "address2":"lydiana", "address3":"o", "address4":"", "address5":"", "address6":"", "address7":"", "address8":"", "comment":"" }, "UploadDate":"10/11/2014", "Signature":"" }, { "Type":"D", "Courier":"013", "Description":"Signature Obtained", "Date":"10/11/2014 09:47:05", "Name":"Pretoria", "Franchise":"PRY", "Status":"YES", "StatusDescription":"Signature Obtained - Your parcel has been delivered and signed for.", "CompanyInfo":{ "contactName":"philimon", "company":"Home Delivery", "address1":"44 suikerbos", "address2":"lydiana", "address3":"o", "address4":"", "address5":"", "address6":"", "address7":"", "address8":"", "comment":"" }, "UploadDate":"10/11/2014", "Signature":"http://api.fastway.org/signatures/0312408478f58b8b1d685a148089960f.jpg" } ], "Signature":"http://api.fastway.org/signatures/0312408478f58b8b1d685a148089960f.jpg", "DistributedTo":"Cape Town ", "DistributedDate":"8/08/2014", "Reference":"", "IsOnforward":"0" }'; $trackingInfo = json_decode($jSON, true); foreach($trackingInfo['Scans'] as $key => $value) { echo "<table>"; foreach ($value as $key2 => $value2) { echo "<tr><td>". $key2 ."</td>"; echo "<td>". $value2 ."</td></tr>"; } echo "</table>"; echo ")"; } ?>[/PHP][/SPOILER] [DOUBLEPOST=1416266785,1416266017][/DOUBLEPOST][B]UPDATE[/B]. I managed to figure out how to get individual values, but I have to now figure out how to make the table. [PHP]foreach($trackingInfo['Scans'] as $key => $value) { $count = count($trackingInfo['Scans']); $dates[$count] = $value['Date'] . "<br>"; print_r($dates); }[/PHP] [DOUBLEPOST=1416267598][/DOUBLEPOST][B]UPDATE:[/B] I fixed it. [PHP]$getLabel = htmlspecialchars($_GET['label']); $jSON = file_get_contents('http://api.fastway.org/v2/tracktrace/detail/'. $getLabel .'?api_key=7e54d8c9fc8ca8ea2e5ea057eb85c4c6'); /*$jSON = '{ "result":{ "LabelNumber":"oa0021148274", "Scans":[ { "Type":"P", "Courier":"020", "Description":"Picked up", "Date":"06/11/2014 18:58:14", "Name":"Cape Town", "Franchise":"CPT", "Status":"PPP", "StatusDescription":"Your parcel was picked up", "CompanyInfo":{ "contactName":"", "company":"", "address1":"", "address2":"", "address3":"", "address4":"", "address5":"", "address6":"", "address7":"", "address8":"", "comment":"" }, "UploadDate":"07/11/2014", "Signature":"" }, { "Type":"T", "Courier":"020", "Description":"Pretoria", "Date":"06/11/2014 19:04:30", "Name":"Cape Town", "Franchise":"CPT", "Status":"PRY", "StatusDescription":"Your parcel is currently in transit between our Cape Town and Pretoria depots.", "CompanyInfo":{ "contactName":"", "company":"", "address1":"", "address2":"", "address3":"", "address4":"", "address5":"", "address6":"", "address7":"", "address8":"", "comment":"" }, "UploadDate":"07/11/2014", "Signature":"" }, { "Type":"T", "Courier":"011", "Description":"Pretoria Depot Scan ", "Date":"10/11/2014 06:56:34", "Name":"Pretoria", "Franchise":"PRY", "Status":"O01", "StatusDescription":"Your parcel has been received in the Pretoria depot.", "CompanyInfo":{ "contactName":"", "company":"", "address1":"", "address2":"", "address3":"", "address4":"", "address5":"", "address6":"", "address7":"", "address8":"", "comment":"" }, "UploadDate":"10/11/2014", "Signature":"" }, { "Type":"T", "Courier":"013", "Description":"Onboard", "Date":"10/11/2014 07:51:19", "Name":"Pretoria", "Franchise":"PRY", "Status":"ONB", "StatusDescription":"Onboard - the parcel is onboard the Courier vehicle.", "CompanyInfo":{ "contactName":"", "company":"Home Delivery", "address1":"44 suikerbos", "address2":"lydiana", "address3":"o", "address4":"", "address5":"", "address6":"", "address7":"", "address8":"", "comment":"" }, "UploadDate":"10/11/2014", "Signature":"" }, { "Type":"D", "Courier":"013", "Description":"Signature Obtained", "Date":"10/11/2014 09:47:05", "Name":"Pretoria", "Franchise":"PRY", "Status":"YES", "StatusDescription":"Signature Obtained - Your parcel has been delivered and signed for.", "CompanyInfo":{ "contactName":"philimon", "company":"Home Delivery", "address1":"44 suikerbos", "address2":"lydiana", "address3":"o", "address4":"", "address5":"", "address6":"", "address7":"", "address8":"", "comment":"" }, "UploadDate":"10/11/2014", "Signature":"http://api.fastway.org/signatures/0312408478f58b8b1d685a148089960f.jpg" } ], "Signature":"http://api.fastway.org/signatures/0312408478f58b8b1d685a148089960f.jpg", "DistributedTo":"Cape Town", "DistributedDate":"8/08/2014", "Reference":"", "IsOnforward":"0" }, "generated_in":"187ms" }';*/ @$trackingInfo = json_decode($jSON, true); if(strpos($jSON, 'does not have any scans') === false) { if(isset($jSON) && !empty($jSON)) { echo " <style> td { padding: 7px 15px 7px 15px; } tr:nth-child(even) { background-color: #ccc; } tr:nth-child(odd) { background-color: #fff; } </style> "; echo "<h2>Tracking Number:<span style='font-weight:normal'> ". $trackingInfo['result']['LabelNumber'] ."</span></h2>"; echo "<table style='border: 1px solid #ccc;'>"; echo "<tr><td><h4>Date: </h4></td>"; echo "<td><h4>Status: </h4></tr>"; foreach($trackingInfo['result']['Scans'] as $key => $value) { $count = count($trackingInfo['Scans']); $dates[$count] = $value['Date']; $desc[$count] = $value['StatusDescription']; $signature[$count] = $value['Signature']; echo "<tr><td>". $dates[$count] ."</td>"; echo "<td>". $desc[$count] ."</td></tr>"; if(!empty($signature[$count])) { $siggy = $signature[$count]; echo "<tr><td>Signature</td><td><img src='". $siggy ."' alt='signature' /></td></tr>"; } } echo "</table>"; } else { echo "<h2>POST data not found. Please resubmit the form.</h2>"; return false; } } else { echo "<h2>The label you have used does not exist. If you believe this is an error, please contact us.</h2>"; return false; } [/PHP] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Programming Q&A
Creating a Table From Specific PHP Array Keys
Top