0

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....

1
  • Hi James. I notice your posts are rather on the chatty side, and on Stack Overflow the community tends to discourage this. Advance thanks, explicit requests to please help, hopes that someone can help, and so forth are best omitted. Would you kindly refrain from this material? It would save volunteer editors a fair bit of future work. Editing one post takes a minute or two, but multiplied by the hundreds of questions one person can ask, it soon mounts up (and we don't have enough volunteer editors). Commented Sep 19, 2017 at 23:06

1 Answer 1

3

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>";
}

Demo

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks mate, had to change a couple of things in my source but it is working! thanks a million! Going to try and work out how to add that additional loop for the other years you supplied as well but thanks for your help on this! :)
It's no problem! Check the edit, it might be better for you if you prefer working with arrays. Good luck!
Will have a play mate, think i could be cheeky and ask if could add that other foreach for the other years for me as just tried and broke it haha
Check this out. Formatting is up to you though :). The foreachs are fairly repetitive, just keep giving it a try, seeing what the compiler yells at you and fixing it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.