KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

I need to connect with API and pass that data in JSON to create a reports DATASTORE #2345

Closed Javier Gallardo opened this topic on on Sep 20, 2021 - 29 comments

Javier Gallardo commented on Sep 20, 2021

I need good help to be able to take data in JSON and show it in tables I need examples, I have been trying for two weeks and I still manage to do it since you do not have any documentation, really the forum seems very bad

Javier Gallardo commented on Sep 20, 2021

I need the report.php and report.view.php files

KoolReport commented on Sep 20, 2021

There are 3 steps that needs to be taken:

  1. Get JSON from your API
  2. Convert JSON to array
  3. Load array into ArrayDataSource

For the number 1, you need to do it by yourself, only you know how your API is called, we do not know. With alot of sample code online, you can curl to your API and get JSON. This is general PHP and nothing to do with KoolReport

For number 2, when you can get JSON, use $arr = json_encode($json,true); to convert your json (string format) to array.

For number 3, you do $this->src("your-array-source-name")->load($arr) to put array into data stream. The $arr feed into load() function need to be in format array of associate array, for example:

[
    ["name"=>"Peter", "age"=>39],
    ["name"=>"John", "age"=>32],
]

We have tried our best to help you. If your question is general, our answer will be general. If you need to be more specific, please give us more specific context, more specific question. Showing your code, the error you faced. Your post like "????" or "it does not work" will not help. When you posted here, we already known something does not work, because it works, you do not need to open topic. It is not important that it does not work, but in what way it does not work.

Javier Gallardo commented on Sep 20, 2021

I only ask you for a documentation of reportboard.php report.php report.view.php

Javier Gallardo commented on Sep 20, 2021

...

Javier Gallardo commented on Sep 20, 2021

RerportBoard.view.php

<?php

namespace space\report;

use \koolreport\widgets\koolphp\Table;
use \koolreport\widgets\google\LineChart;

?> <div class="report-content">

<div class="text-center">
</div>

<?php //Use associate array

        Table::create(array(
            "dataSource"=>$this->dataStore("apiarray"),
            "cssClass"=>array(
                "table"=>"table-bordered table-striped table-hover"
            )
        ));
?>

</div>

Javier Gallardo commented on Sep 20, 2021

Javier Gallardo commented on Sep 20, 2021

My JSON

{
  "projects": [
    {
      "id": 1,
      "name": "mantisbt",
      "status": {
        "id": 10,
        "name": "development",
        "label": "development"
      },
      "description": "Mantis Project",
      "enabled": true,
      "view_state": {
        "id": 10,
        "name": "public",
        "label": "public"
      },
      "access_level": {
        "id": 90,
        "name": "administrator",
        "label": "administrator"
      },
      "custom_fields": [
        {
          "id": 8,
          "name": "Email",
          "type": "email",
          "default_value": "",
          "possible_values": "",
          "valid_regexp": "",
          "length_min": 0,
          "length_max": 0,
          "access_level_r": {
            "id": 10,
            "name": "viewer",
            "label": "viewer"
          },
          "access_level_rw": {
            "id": 10,
            "name": "viewer",
            "label": "viewer"
          },
          "display_report": true,
          "display_update": true,
          "display_resolved": true,
          "display_closed": true,
          "require_report": false,
          "require_update": false,
          "require_resolved": false,
          "require_closed": false
        }
      ],
      "versions": [
        {
          "id": 258,
          "name": "2.0.x",
          "description": "",
          "released": false,
          "obsolete": false,
          "timestamp": "2016-10-02T18:45:04-04:00"
        }
      ],
      "subProjects": [
        {
          "id": 31,
          "name": "SubProject1"
        }
      ]
    }
  ]
}
Javier Gallardo commented on Sep 20, 2021

I can't find anything on how to extract parts of this JSON in interntet :(

Javier Gallardo commented on Sep 21, 2021

this is the json when i pass it through array

print_r(json_decode($jsoon, true));

Array ( [projects] => Array ( [0] => Array ( [id] => 1 [name] => mantisbt [status] => Array ( [id] => 10 [name] => development [label] => development ) [description] => Mantis Project [enabled] => 1 [view_state] => Array ( [id] => 10 [name] => public [label] => public ) [access_level] => Array ( [id] => 90 [name] => administrator [label] => administrator ) [custom_fields] => Array ( [0] => Array ( [id] => 8 [name] => Email [type] => email [default_value] => [possible_values] => [valid_regexp] => [length_min] => 0 [length_max] => 0 [access_level_r] => Array ( [id] => 10 [name] => viewer [label] => viewer ) [access_level_rw] => Array ( [id] => 10 [name] => viewer [label] => viewer ) [display_report] => 1 [display_update] => 1 [display_resolved] => 1 [display_closed] => 1 [require_report] => [require_update] => [require_resolved] => [require_closed] => ) ) [versions] => Array ( [0] => Array ( [id] => 258 [name] => 2.0.x [description] => [released] => [obsolete] => [timestamp] => 2016-10-02T18:45:04-04:00 ) ) [subProjects] => Array ( [0] => Array ( [id] => 31 [name] => SubProject1 ) ) ) ) )

KoolReport commented on Sep 21, 2021

You see, the json returned is very specific structure for that API only so you can not expect to find copy-and-paste code anywhere in internet. You need to write your own code to extract information from API and transform it into a suitable table format.

You need to convert your code into array of associate array:

$arr = json_decode($json, true);
$result = [];
foreach($arr["projects"] as $project)
{
    array_push($result,[
        "id"=>$arr["projects"]["id"],
        "name"=>$arr["projects"]["name"],
        "status.name"=>$arr["projects"]["status"]["name"]
    ]);
}

$this->src("array-datasource-name")->load($result)
...

I have made you an example code of how to extract your json into $result. I have only use id,name and status.name to demonstrate the extraction. The rest you need to do for yourself, just adding more column to result array.

Javier Gallardo commented on Sep 24, 2021

how could I call the $result data to be reflected in report.view.php

Javier Gallardo commented on Sep 24, 2021
<?php

namespace space\report;

    use \koolreport\widgets\koolphp\Table;
    use \koolreport\widgets\google\LineChart;
?>
<div class="report-content">
    <div class="text-center">
        <h1>Join</h1>
        <p class="lead">Primer Grafico</p>
    </div>
    

            <h5 class="text-center">Primera tabla</h5>
   <?php
//Use associate array
            Table::create(array(
                "dataSource"=>$this->dataStore("array-datasource-name"),
                "cssClass"=>array(
                    "table"=>"table-bordered table-striped table-hover"
                )
            ));
    ?>
</div>
KoolReport commented on Sep 24, 2021

You save the result into a report variable:

$arr = json_decode($json, true);
$result = [];
foreach($arr["projects"] as $project)
{
    array_push($result,[
        "id"=>$arr["projects"]["id"],
        "name"=>$arr["projects"]["name"],
        "status.name"=>$arr["projects"]["status"]["name"]
    ]);
}

$this->result = $result; //<-- do this 

Later in the report.view.php:

<?php

namespace space\report;

    use \koolreport\widgets\koolphp\Table;
    use \koolreport\widgets\google\LineChart;

    //You can access through $this->result

    $result = $this->result;

    var_dump($result);
?>
<div class="report-content">
    <div class="text-center">
        <h1>Join</h1>
        <p class="lead">Primer Grafico</p>
    </div>
    

            <h5 class="text-center">Primera tabla</h5>
   <?php
//Use associate array
            Table::create(array(
                "dataSource"=>$this->dataStore("array-datasource-name"),
                "cssClass"=>array(
                    "table"=>"table-bordered table-striped table-hover"
                )
            ));
    ?>
</div>
Javier Gallardo commented on Sep 24, 2021

Javier Gallardo commented on Sep 24, 2021
<?php

namespace space\report;

    use \koolreport\widgets\koolphp\Table;
    use \koolreport\widgets\google\LineChart;

    //You can access through $this->result

     $result = $this->result;

    var_dump($result);
?>
<div class="report-content">
    <div class="text-center">
        <h1>Join</h1>
        <p class="lead">Primer Grafico</p>
    </div>
    

            <h5 class="text-center">Primera tabla</h5>
   <?php

//Use associate array
            Table::create(array(
        
    "dataSource"=>$this->dataStore("MAYBE THIS PART I NO LONGER NEED IT  "),
                "cssClass"=>array(
                    "table"=>"table-bordered table-striped table-hover"
                )
            ));
    ?>
</div>
Javier Gallardo commented on Sep 24, 2021

Please create a document on this for almost three weeks, I try to compare it with the current documentation I read and read the documents and I can't quite understand how the hell to do it, I am not an expert in php, so I hired the service greetings

Javier Gallardo commented on Sep 24, 2021

How much more money should I pay you for personalized advice?

Javier Gallardo commented on Sep 24, 2021

...

Javier Gallardo commented on Sep 24, 2021

...

KoolReport commented on Sep 27, 2021

I did not realize that you are using Dashboard Framework, not normal Koolreport. From the error you sent me, I guess you are using CustomBoard. Would you mind to post your code of ReportBoard.php and also ReportBoard.view.php. I would like to see where you put the API connection code.

Javier Gallardo commented on Sep 27, 2021

report.php

<?php

namespace space\report;

use \koolreport\processes\ColumnMeta;
use \koolreport\processes\Filter;

//Step 2: Creating Report class
class MyReport extends \koolreport\KoolReport
{
    protected function settings()
    {
    return array(
        "dataSources"=>array(
            "apiarray"=>array(
                "class"=>'\koolreport\datasources\ArrayDataSource',
                "dataFormat"=>"table",        
            )
        )
    );
    }
    protected function setup()
    {
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://demo.owlsecurity.cl/api/rest/projects',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => array(
    'Authorization: dQ-a3-atW78S6HvpXhnpnp8Aj8xMqxwY'
  ),
));

$response = curl_exec($curl);
curl_close($curl);
$json = $response;
$arr = json_decode($json, true);
$result = [];
foreach($arr["projects"] as $project)
{
    array_push($result,[
        "id"=>$arr["projects"]["id"],
        "name"=>$arr["projects"]["name"],
        "status.name"=>$arr["projects"]["status"]["name"]
    ]);
}

$this->src("array-datasource-name")->load($result)
    }
}
Javier Gallardo commented on Sep 27, 2021

reportboard.view

<?php

namespace space\report;

    use \koolreport\widgets\koolphp\Table;
    use \koolreport\widgets\google\LineChart;

    //You can access through $this->result

    $result = $this->result;

    var_dump($result);
?>
<div class="report-content">
    <div class="text-center">
        <h1>Join</h1>
        <p class="lead">Primer Grafico</p>
    </div>
    

            <h5 class="text-center">Primera tabla</h5>
   <?php
//Use associate array
            Table::create(array(
                "dataSource"=>$this->dataStore("array-datasource-name"),
                "cssClass"=>array(
                    "table"=>"table-bordered table-striped table-hover"
                )
            ));
    ?>
</div>
KoolReport commented on Sep 28, 2021

I don't understand. Your previous error mentions the file ReportBoard.view.php and you seems to use koolreport\dashboard\CustomBoard but you post me the code of report.php which derived from koolreport\KoolReport. Are you mixing something here?

Javier Gallardo commented on Sep 28, 2021

I do not understand your question

KoolReport commented on Sep 28, 2021

Previously you post to my the file report.php which is not correct. Please post me 2 files:

  1. ReportBoard.php
  2. ReportBoard.view.php
Javier Gallardo commented on Sep 28, 2021

ReportBoard

<?php

namespace space\report;

use \koolreport\dashboard\CustomBoard;

class ReportBoard extends CustomBoard {

}

Javier Gallardo commented on Sep 28, 2021

ReportBoard.view

<?php

namespace space\report;

use \koolreport\widgets\koolphp\Table;
use \koolreport\widgets\google\LineChart;

//You can access through $this->result

$result = $this->result;

var_dump($result);

?> <div class="report-content">

<div class="text-center">
    <h1>Join</h1>
    <p class="lead">Primer Grafico</p>
</div>


        <h5 class="text-center">Primera tabla</h5>

<?php //Use associate array

        Table::create(array(
            "dataSource"=>$this->dataStore("array-datasource-name"),
            "cssClass"=>array(
                "table"=>"table-bordered table-striped table-hover"
            )
        ));
?>

</div>

KoolReport commented on Sep 30, 2021

You do something like this:

ReportBoard.php

<?php

namespace space\report;

use \koolreport\dashboard\CustomBoard;

class ReportBoard extends CustomBoard
{
    protected function actionIndex($request, $response)
    {
        $curl = curl_init();

        curl_setopt_array($curl, array(
          CURLOPT_URL => 'https://demo.owlsecurity.cl/api/rest/projects',
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => '',
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 0,
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => 'GET',
          CURLOPT_HTTPHEADER => array(
            'Authorization: dQ-a3-atW78S6HvpXhnpnp8Aj8xMqxwY'
          ),
        ));
        
        $response = curl_exec($curl);
        curl_close($curl);
        $json = $response;
        $arr = json_decode($json, true);
        $result = [];
        foreach($arr["projects"] as $project)
        {
            array_push($result,[
                "id"=>$arr["projects"]["id"],
                "name"=>$arr["projects"]["name"],
                "status.name"=>$arr["projects"]["status"]["name"]
            ]);
        }

        $this->renderView([
            "result"=>$result
        ]);
    }
}

In the view file ReportBoard.view.php

<?php
    use \koolreport\widgets\koolphp\Table;
    use \koolreport\widgets\google\LineChart;

    $result = $this->params()["result"];
    var_dump($result);
?>
...
Javier Gallardo commented on Sep 30, 2021

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
None yet

None