0

I'm trying to create an html table based on the results from an array from a GET request. I have tried for loops and I have tried Java examples, but the results are always displayed as a long string (or if I return the results as dd($response) it only returns one row. I was wondering if there is a problem with the way format the array is returned:

{   "results":
[

    {   
        "column1":"TEST1",
        "column2":"DATADATADATA",   
        "time":"2017-02-27T16:25:03.1230000Z"
    },
    {   
        "column1":"TEST2",
        "column2":"DATADATADATA",
        "time":"2017-07-03T02:48:29.8300000Z"
    },
    {
        "column1":"TEST3",
        "column2":"DATADATADATA",
        "time":"2017-07-19T15:09:27.0900000Z"}
]

}

This is one example I tried in PHP:

$reponse = array(print_r($response));


for ($i = 0; $i < count($reponse); $i++) {
    for ($l = 0; $l < count($reponse[$i]); $l++) {
        echo $reponse[$i][$l];
        echo "<br/>";
    };
};
5
  • 3
    Show us what you tried! Commented Jul 19, 2017 at 15:49
  • This is one example that I tried (only returns one long string):$reponse = array(print_r($response)); for ($i = 0; $i < count($reponse); $i++) { for ($l = 0; $l < count($reponse[$i]); $l++) { echo $reponse[$i][$l]; echo "<br/>"; }; }; Commented Jul 19, 2017 at 15:50
  • Are you wanting to build the table in the backend or the frontend? Because your php looks like it will not be returning valid json, which the frontend usually would be expecting. How does the json you included at the top relate to the script at the bottom? Commented Jul 19, 2017 at 15:57
  • @Taplar - the top is the result of $response. Commented Jul 19, 2017 at 16:03
  • So the top json is the json that prints if you do json_encode($response)? Commented Jul 19, 2017 at 16:04

1 Answer 1

2

Use json_decode() to convert JSON to an array:

$array = json_decode($data, true);

Then you'll be able to iterate over it:

@foreach ($array['results'] as $element)
    {{ $element['column1'] }}
    {{ $element['column2'] }}
    {{ $element['time'] }}
@endforeach
Sign up to request clarification or add additional context in comments.

5 Comments

This returns an error: {Parse error: syntax error, unexpected 'foreach' (T_FOREACH)}
disregard, I had it in the wrong place. This get me closer I think, it removes the array elements and only returns the results, but it is still returned as one long (string?).
@user2796515 it's not a long string, {{ $var }} is just displaying the data. You can do <div>{{ $element['column1'] }}</div>, for example. In this case, you'll get many lines. Add whatever HTML layout you want. It's just an example.
Undertsood. I think this will work, I will accept your answer and update if I get it working completely!
I didn't even have to apply CSS once applying your answer. I surrounded them in <div>s with a class and they separated out. Now I can apply some CSS and I am all set. Thank you!

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.