Home > Enterprise >  How to print it in a table form using the loops
How to print it in a table form using the loops

Time:09-01

This data is fetched from CURL in php and after decode I got this result, could you help me to print it in a table form so that I will be able to show it in web page

    <?php
                $user_curl = curl_init();

                curl_setopt_array($user_curl, array(
                    CURLOPT_URL => "https://gorest.co.in/public-api/users",
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_ENCODING => "",
                    CURLOPT_MAXREDIRS => 10,
                    CURLOPT_TIMEOUT => 60,
                    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                    CURLOPT_CUSTOMREQUEST => "GET",
                    CURLOPT_POSTFIELDS => "",
                    CURLOPT_HTTPHEADER => array(
                        "cache-control: no-cache",
                        "content-type: application/json",
                    ),
                ));


                $user_curl_response = curl_exec($user_curl);
                $user_curl_err = curl_error($user_curl);

                if ($user_curl_response) {

                $result = json_decode($user_curl_response,true);

                 echo "<pre>";
                 print_r($result);

                    
                } else {
                    echo 'No data found';
                }
Array
(
    [code] => 200
    [meta] => Array
        (
            [pagination] => Array
                (
                    [total] => 4018
                    [pages] => 402
                    [page] => 1
                    [limit] => 10
                )

        )

    [data] => Array
        (
            [0] => Array
                (
                    [id] => 4092
                    [name] => Dhanu Nambeesan
                    [email] => [email protected]
                    [gender] => male
                    [status] => inactive
                )

            [1] => Array
                (
                    [id] => 4091
                    [name] => Sen. Ekadant Reddy
                    [email] => [email protected]
                    [gender] => male
                    [status] => inactive
                )

            [2] => Array
                (
                    [id] => 4089
                    [name] => Narinder Acharya III
                    [email] => [email protected]
                    [gender] => female
                    [status] => inactive
                )

            [3] => Array
                (
                    [id] => 4088
                    [name] => Fr. Agastya Saini
                    [email] => [email protected]
                    [gender] => male
                    [status] => inactive
                )

            [4] => Array
                (
                    [id] => 4087
                    [name] => Bhuvaneshwar Dwivedi
                    [email] => [email protected]
                    [gender] => male
                    [status] => inactive
                )

            [5] => Array
                (
                    [id] => 4086
                    [name] => Gov. Gorakhanatha Varma
                    [email] => [email protected]
                    [gender] => female
                    [status] => inactive
                )

            [6] => Array
                (
                    [id] => 4085
                    [name] => Kali Bhat MD
                    [email] => [email protected]
                    [gender] => female
                    [status] => inactive
                )

            [7] => Array
                (
                    [id] => 4083
                    [name] => Uttam Gupta
                    [email] => [email protected]
                    [gender] => female
                    [status] => active
                )

            [8] => Array
                (
                    [id] => 4082
                    [name] => Krishna Arora
                    [email] => [email protected]
                    [gender] => male
                    [status] => active
                )

            [9] => Array
                (
                    [id] => 4081
                    [name] => Gov. Aruna Abbott
                    [email] => [email protected]
                    [gender] => female
                    [status] => active
                )

        )

)

CodePudding user response:

This is the basic idea.

$headers = array_keys($data['data'][0]) ?? null;

$headerHtml = "<tr>";
foreach($headers as $header) {
    $headerHtml.= "<th><strong>{$header}<strong></th>";
}
$headerHtml.= "</tr>";


$tableHtml = "";
foreach($data['data'] as $values) {
    $tableHtml.= "<tr>";
    foreach($values as $value) {
        $tableHtml.= "<td>{$value}</td>";
    }
    $tableHtml.= "</tr>";
}

echo "<div>
    <table>
        {$headerHtml}
        {$tableHtml}
    </table
</div>";

CodePudding user response:

From what I understand , you want to be able to get it like this: $result['id'], in that case you need to sort your data , and use the following foreach to get you to 'id':

         foreach($result["data"] as $results){
            echo "<pre>".$results["id"]."</pre>";
         }
  • Related