Reputation: 1706
I just started to learn PHP and I have managed to get JSON data back that I need but struggling to build a table from the JSON data. I know I am doing it totally wrong with trial and error, but stuck now.
The PHP I have so far:
<?php
............
$domains = json_decode(get_option('cc_whmcs_bridge_tlds'), true);
if (count($domains->pricing)) {
// Open the table
echo "<table>";
// Cycle through the array
foreach ($domains->pricing as $idx => $tld) {
// Output a row
echo "<tr>";
echo "<td>$tld->register[$idx]</td>";
echo "<td>$tld->transfer->[$idx]</td>";
echo "</tr>";
}
// Close the table
echo "</table>";
}
?>
JSON OUTPUT EXAMPLE
{
"currency": {
"id": "1",
"code": "USD",
"prefix": "$",
"suffix": " USD",
"format": "2",
"rate": "1.00000"
},
"pricing": {
"com": {
"categories": [
"Popular",
"gTLD"
],
"addons": {
"dns": true,
"email": true,
"idprotect": true
},
"group": "new",
"register": {
"1": "9.95",
"2": "19.90",
"3": "29.85"
},
"transfer": {
"1": "9.95",
"2": "15.00",
"3": "25.00"
},
"renew": {
"1": "9.95",
"2": "15.00",
"3": "25.00"
}
},
"net": {
"categories": [
"Popular",
"gTLD"
],
"addons": {
"dns": false,
"email": false,
"idprotect": false
},
"group": "sale",
"register": {
"1": "9.00"
},
"transfer": {
"1": "11.95"
},
"renew": {
"1": "11.95"
}
},
"org": {
"categories": [
"Popular",
"gTLD"
],
"addons": {
"dns": false,
"email": false,
"idprotect": false
},
"group": "hot",
"register": {
"1": "11.95"
},
"transfer": {
"1": "11.95"
},
"renew": {
"1": "11.95"
}
}
}
}
I know I have got the table PHP stuff totally wrong but as I said, first time for me so that is as far as I got.
The table I am trying to render would be something like:
TLD | REGISTER | TRANSFER | RENEW
---------------------------------------------
.com | 1yr (9.95) | 1yr (9.95) | 1yr (9.95)
.co.uk | 1yr (9.95) | 1yr (9.95) | 1yr (9.95)
etc....
Upvotes: 0
Views: 75
Reputation: 9957
The problem you're having is that the elements inside the loop are not arrays, but objects (instances of stdClass
, specifically). You can continue going through them using the arrow operator:
$domains = json_decode($json);
foreach ($domains->pricing as $tld => $attrs) {
echo "<tr>";
echo "<td>".$tld."</td>";
echo "<td>1yr (".$attrs->register->{1}.")</td>";
echo "<td>1yr (".$attrs->transfer->{1}.")</td>";
echo "<td>1yr (".$attrs->renew->{1}.")</td>";
echo "</tr>";
}
And you can keep going at it the same way. For example, if you need to show all the price options for different years per type, inside the loop you can add this:
foreach ($attrs->register as $nYears => $pricePerYear) {
echo $nYears." yrs: ".$pricePerYear;
}
And your other option, which is closer to what you had initially, is setting true
as the second parameter for json_decode()
, which will give you an array
instead of an stdClass
instance. This code accomplishes the same:
$domains = json_decode($json, true);
foreach ($domains["pricing"] as $tld => $attrs) {
echo "<tr>";
echo "<td>".$tld."</td>";
echo "<td>1yr (".$attrs["register"][1].")</td>";
echo "<td>1yr (".$attrs["transfer"][1].")</td>";
echo "<td>1yr (".$attrs["renew"][1].")</td>";
echo "</tr>";
}
So you can try working it that way, whichever feels more comfortable for you.
Upvotes: 3