Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines
There are 3 steps that needs to be taken:
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.
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>
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"
}
]
}
]
}
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 ) ) ) ) )
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.
<?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>
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>
<?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>
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.
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)
}
}
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>
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>
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);
?>
...
Let KoolReport help you to make great reports. It's free & open-source released under MIT license.
Download KoolReport View demo