KoolReport's Forum

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

How to make looping in table to show desired columns? #146

Open tee opened this topic on on Nov 3, 2017 - 9 comments

tee commented on Nov 3, 2017

Hi support team,

My problem is in order to avoid using hard code to define which columns' name and labels to be displayed in view, i am thinking is it possible to use dynamical way to loop out all desired columns and label name from queryB. My queryA from is Select * from tableA and queryB is Select col_name, field_name from tableB where.... QueryB will select all those desired columns and field name. What i have done so far was only display one column in view. Let say my queryB returns 5 records and i want to loop 5 times to display that 5 columns and field name, how could i done it? Thanks.

<?php 
    $col_name = $this->dataStore("queryB")->data()[0]["col_name"];
    $field_name = $this->dataStore("queryB")->data()[0]["field_name "];

    Table::create(array(
        "dataStore"=>$this->dataStore('queryA'),
        "columns"=>array(
            $col_name=>array(
                "label"=>$field_name 
            ),
        ),
        "cssClass"=>array(
            "table"=>"table table-bordered"
        )
    ));
?>
KoolReport commented on Nov 3, 2017

That's easy actually, you do this:

$columns = array();
$data_queryB = $this->dataStore('queryB')->data();

foreach($data_queryB as $row)
{
    $columns[$row["col_name"]] = array(
        "label"=>$row["field_name"]
    );
}

Table::create(array(
    ...
    "columns"=>$columns,
));
tee commented on Nov 3, 2017

Show error Undefined index: col_name

KoolReport commented on Nov 3, 2017

Tee, I take that name from your above code $this->dataStore("queryB")->data()[0]["col_name"] => So I suppose that you have the "col_name" . Take my code as the guide or direction only, you may need to debug the code to see where went wrong.

tee commented on Nov 6, 2017

I have that column named col_name but still show same error.

tee commented on Nov 6, 2017

Below is my Report and Report view. Thanks.

Report:

function setup() 
    {    
        $this->src('a') 
        ->query("SELECT col_name, fieldname FROM queryB where guid = @guid ")
        ->params(array(
            '@guid' => $this->params["guid"],
        ))
        ->pipe($this->dataStore("queryB"));

        $this->src('a')
        ->query("SELECT * FROM queryA")
        ->pipe($this->dataStore("queryA"));
    }

Report view:

        $columns = array();
        $data_field = $this->dataStore('queryB')->data();

        foreach($data_field as $row)
        {
            $columns['col_name'] = array(
                "label" => $row["field_name"],

            );
        }

        Table::create(array(
        "dataStore" => $this->dataStore('queryA'),
        "columns" => $columns,
        "cssClass" => array(
            "table" => "table table-striped table-bordered table-hover table-condensed",
            "td" => "right-align",
            "tf" => "tfcolor",
            "th" => "color",
            "td" => function($row,$columnName){
            switch($columnName)
                {
                case "field_name":
                    return "col-color";
                default:
                    return "";
                }
            },
        ),
    ));

KoolReport commented on Nov 6, 2017

Change it to:

        $columns[$row["col_name"]] = array(
            "label" => $row["field_name"],

        );
tee commented on Nov 6, 2017

It works, thanks a lot.

KoolReport commented on Nov 6, 2017

Great!

vudoc commented on Mar 31, 2019

My Current view is like this

class MyReport extends \koolreport\KoolReport {

use \koolreport\laravel\Friendship;
use \koolreport\cloudexport\Exportable;   
//We leave this blank to demo only
function setup()
{
 $min = $this->params["min"];
 $agen = $this->params["agen"];
 $sec = $this->params["sec"];
 $sub_sec = $this->params["sub_sec"];
 
 $this->src("mysql")->query(MySQL::type(
        DB::table("demands")->join('approved_project_info', 'approved_project_info.unapprove_project_id', '=', 'demands.project_id')
       ->where('ministry', $min)            
        ))
    ->pipe($this->dataStore('demands'));
   
}

} My view code is here, <!-- MyReport.view.php --> <?php use \koolreport\widgets\koolphp\Table; ?>

     <?php
     
    Table::create([
        "dataSource"=>$this->dataStore("demands")
    ]);
    ?>

But i need to show only two column from the view which is (demand_type and project_code) my database name is (demands) what should i need to do for change in view.

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
help needed

None