KoolReport's Forum

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

Undefined index and array_merge error in table create for pdf #2817

Open Andrew Guattery opened this topic on on Sep 3, 2022 - 9 comments

Andrew Guattery commented on Sep 3, 2022

Ok gang, working on export to pdf, create table looks like this:

   Table::create(array(
        "dataSource"=>$this->dataStore('paybyp2'),
           "columns"=> [
        "HAS_COMMNTS"=>[
    "label"=>"",
    'formatValue'=>function($value, $row, $cKey) {
        if($row["HAS_COMMNTS"] == "*"){
        return '<i class="far fa-plus-square expand-collapse-detail-icon" aria-hidden="true"></i>';
        }else{
        return '';
        }
        }
    ],
    
    "STR_ID"=>[
    "label"=>"Store",
    "format"=>"string"],
    "USR_ID"=>[
    "label"=>"User ID",

    ],

    "PERIOD_DT"=>[
    "label"=>"Clock Date",
    "displayFormat"=>"m-d-Y"
     ],
     "SEQ_NO_CLCK_IN"=>[
    
     ],
    "SEQ_NO_CLCK_OUT"=>[
     
     ],
    "DISP_CLCK_IN"=>[   
    "label"=>"Clock In",
    ],
    "DISP_CLCK_OUT"=>[
    "label"=>"Clock Out",

],
    
    "IsHoliday"=>[
        "label"=>"Holiday",
    'formatValue'=>function($value, $row, $cKey) {
        if($row["IsHoliday"] == 1){
        return 8;
        }else{
        return 0;
        }
        }
    ],

    "BREAK_TIM"=>[
    "label"=>"Breaks",
        "formatValue" => function($value, $row, $cKey) {
    return max(number_format($value/60, 3, '.', ','),0);
    //return ($row['MINUTES_WRKD'] + $row['PTO_TO_PAY']) - $row['BREAK_TIM'];
    },
    ],
    "MEAL_TIM"=>[
    "label"=> $this->dataStore("mealspd")->only("MEAL_BRK_PD")->data()[0]["MEAL_BRK_PD"] == 'Y' ? "Meals(pd)" : "Meals",
        "formatValue" => function($value, $row, $cKey) {
        if($this->dataStore("mealspd")->only("MEAL_BRK_PD")->data()[0]["MEAL_BRK_PD"] == 'Y'){
        return max(number_format($value/60, 3, '.', ','),0);
        }else{
        return 0;
        }
    
    //return ($row['MINUTES_WRKD'] + $row['PTO_TO_PAY']) - $row['BREAK_TIM'];
    },
    ],
    "PTO_TO_PAY"=>[
    "label"=>"PTO",
        "formatValue" => function($value, $row, $cKey) {
    return max(number_format($value/60, 3, '.', ','),0);
    //return ($row['MINUTES_WRKD'] + $row['PTO_TO_PAY']) - $row['BREAK_TIM'];
    },
    ],
    "MINUTES_WRKD"=>[
    "label"=>"Hours Worked",
    "formatValue" => function($value, $row, $cKey) {
    return max(number_format($value/60, 3, '.', ','),0);
    //return ($row['MINUTES_WRKD'] + $row['PTO_TO_PAY']) - $row['BREAK_TIM'];
    },
    ],
    "USRNAME"=>[
    "label"=>"user",
    "visible"=>false
    ],
    "TOTALPD"=>[
    "label"=>"Total",
    "formatValue" => function($value, $row, $cKey) {
    //return max(number_format($value, 2, '.', ' '),0);
    $hol = $row['IsHoliday'] == 1 ? 480 : 0;
    return number_format((($row['MINUTES_WRKD'] + $row['PTO_TO_PAY'] + $hol) - $row['BREAK_TIM'])/60,3,'.',',');
    },
    ],
    ],
        "grouping"=>array(
            "USRNAME"=>array(
                "calculate"=>array(
                    "{sumAmount}"=>array("sum","MINUTES_WRKD")
                ),
                "top"=>"<b> {USRNAME}</b>",
                "bottom"=>"<td><b>Total {USRNAME}</b></td><td><b>{sumAmount}</b></td>",
            ),
        ),
        "sorting"=>array(
            "USRNAME"=>"asc"
        ),
        "showFooter"=>true,

        "cssClass"=>array(
            "table"=>"table-bordered",
            "tf"=>"darker"
        )
    ));

When I click the button to export to browser, the report shows up with data but the column labels do not show and i get the following php error:

[03-Sep-2022 12:10:29 America/New_York] PHP Notice:  Undefined index: USRNAME in C:\inetpub\wwwteam\koolreport\pro_5.16.2\koolreport\core\src\widgets\koolphp\Table.php on line 397
[03-Sep-2022 12:10:29 America/New_York] PHP Warning:  array_merge(): Expected parameter 1 to be an array, null given in C:\inetpub\wwwteam\koolreport\pro_5.16.2\koolreport\core\src\widgets\koolphp\Table.php on line 397
[03-Sep-2022 12:10:29 America/New_York] PHP Notice:  Undefined index: MINUTES_WRKD in C:\inetpub\wwwteam\koolreport\pro_5.16.2\koolreport\core\src\widgets\koolphp\Table.php on line 397
[03-Sep-2022 12:10:29 America/New_York] PHP Warning:  array_merge(): Expected parameter 1 to be an array, null given in C:\inetpub\wwwteam\koolreport\pro_5.16.2\koolreport\core\src\widgets\koolphp\Table.php on line 397

I get this for each column listed - The columns are correct, any idea why this is happening?

KoolReport commented on Sep 4, 2022

Could you please check if the key in your data is USRNAME or "usrname", the key is case-sensitive so it could be the reason of Undefined Index error showed.

Andrew Guattery commented on Sep 4, 2022

They are all capital letters. If it makes a difference these are column names from MSSQL Server.

Andrew Guattery commented on Sep 6, 2022

More of an update: I cut the Table::Create down to the following for testing:

    Table::create(array(
        "dataSource"=>$this->dataStore('paybyp2'),
           "columns"=> [

    "STR_ID"=>[
    "label"=>"Store",
    "format"=>"string"],
    "USR_ID"=>[
    "label"=>"User ID",

    ],

    "PERIOD_DT"=>[
    "label"=>"Clock Date",
    "displayFormat"=>"m-d-Y"
     ],

    "MINUTES_WRKD"=>[
    "label"=>"Hours Worked",
    "formatValue" => function($value, $row, $cKey) {
    return max(number_format($value/60, 3, '.', ','),0);
    //return ($row['MINUTES_WRKD'] + $row['PTO_TO_PAY']) - $row['BREAK_TIM'];
    },
    ],
    "USRNAME"=>[
    "label"=>"user",
    "visible"=>false
    ],

    ],
        "grouping"=>array(
            "USRNAME"=>array(
                "calculate"=>array(
                    "{sumAmount}"=>array("sum","MINUTES_WRKD")
                ),
                "top"=>"<b> {USRNAME}</b>",
                "bottom"=>"<td><b>Total {USRNAME}</b></td><td><b>{sumAmount}</b></td>",
            ),
        ),
        "sorting"=>array(
            "USRNAME"=>"asc"
        ),
        "showFooter"=>true,

        "cssClass"=>array(
            "table"=>"table-bordered",
            "tf"=>"darker"
        )
    ));

And here is the Export.php:

require_once "../../../koolreport/pro_5.16.2/koolreport/core/autoload.php";
require_once "Payroll.php";
$report = new Payroll(array(
    "store"=>$_GET['store'],
    "employees"=>$_GET['employees'],
    "mindt"=>$_GET['mindt'],
    "maxdt"=>$_GET['maxdt']
));
$filename = 'Payroll.pdf';
//$report->params['store'] = $_GET['store'];
 //var_dump($report);
$report->run()
->export('PayrollRptPdf',array(

    "store"=>$_GET['store'],
    "employees"=>$_GET['employees'],
    "mindt"=>$_GET['mindt'],
    "maxdt"=>$_GET['maxdt']
))
->pdf(array(
    "format"=>"letter",
    "orientation"=>"portrait"
))
->toBrowser($filename,true);

And here is the resulting php file screenshot:

Now, in the php error log I have the following:

[06-Sep-2022 09:01:10 America/New_York] PHP Notice:  Undefined index: STR_ID in C:\inetpub\wwwteam\koolreport\pro_5.16.2\koolreport\core\src\widgets\koolphp\Table.php on line 397
[06-Sep-2022 09:01:10 America/New_York] PHP Warning:  array_merge(): Expected parameter 1 to be an array, null given in C:\inetpub\wwwteam\koolreport\pro_5.16.2\koolreport\core\src\widgets\koolphp\Table.php on line 397
[06-Sep-2022 09:01:10 America/New_York] PHP Notice:  Undefined index: USR_ID in C:\inetpub\wwwteam\koolreport\pro_5.16.2\koolreport\core\src\widgets\koolphp\Table.php on line 397
[06-Sep-2022 09:01:10 America/New_York] PHP Warning:  array_merge(): Expected parameter 1 to be an array, null given in C:\inetpub\wwwteam\koolreport\pro_5.16.2\koolreport\core\src\widgets\koolphp\Table.php on line 397
[06-Sep-2022 09:01:10 America/New_York] PHP Notice:  Undefined index: PERIOD_DT in C:\inetpub\wwwteam\koolreport\pro_5.16.2\koolreport\core\src\widgets\koolphp\Table.php on line 397
[06-Sep-2022 09:01:10 America/New_York] PHP Warning:  array_merge(): Expected parameter 1 to be an array, null given in C:\inetpub\wwwteam\koolreport\pro_5.16.2\koolreport\core\src\widgets\koolphp\Table.php on line 397
[06-Sep-2022 09:01:10 America/New_York] PHP Notice:  Undefined index: MINUTES_WRKD in C:\inetpub\wwwteam\koolreport\pro_5.16.2\koolreport\core\src\widgets\koolphp\Table.php on line 397
[06-Sep-2022 09:01:10 America/New_York] PHP Warning:  array_merge(): Expected parameter 1 to be an array, null given in C:\inetpub\wwwteam\koolreport\pro_5.16.2\koolreport\core\src\widgets\koolphp\Table.php on line 397
[06-Sep-2022 09:01:10 America/New_York] PHP Notice:  Undefined index: USRNAME in C:\inetpub\wwwteam\koolreport\pro_5.16.2\koolreport\core\src\widgets\koolphp\Table.php on line 397
[06-Sep-2022 09:01:10 America/New_York] PHP Warning:  array_merge(): Expected parameter 1 to be an array, null given in C:\inetpub\wwwteam\koolreport\pro_5.16.2\koolreport\core\src\widgets\koolphp\Table.php on line 397

Each column is in the error log once for undefined index and once for array_merge; no idea why this is happening. Anyone have any ideas? The column names are correct (all caps/underscores/etc) and as you can see they render in the pdf correctly. Perhaps this is some sort of timing issue? It appears that the column metadata is not in the dataStore before the PDF is rendered. Maybe we need to do something different in export.php before we do the $report->run()?

Andrew Guattery commented on Sep 8, 2022

Ok here is an update: The problem is related to when I pass query parameter to the new report. When I export like this:

$report = new Payroll(
[
    "store"=>$_GET['store'],
    "employees"=>$_GET['employees'],
    "mindt"=>$_GET['mindt'],
    "maxdt"=>$_GET['maxdt']
]
);

The pdf does not get the column names and the errors are logged. If I remove this:

[
    "store"=>$_GET['store'],
    "employees"=>$_GET['employees'],
    "mindt"=>$_GET['mindt'],
    "maxdt"=>$_GET['maxdt']
]

and run it the pdf renders correctly with column names but there is no data due to the missing parameters. This is also happening if I do

$report->run()->render('PayrollRptPdf');

am I doing this wrong, or is this a bug in the current version?

Andrew Guattery commented on Sep 11, 2022

Another update - This is definitely something wrong with the Table widget. I have created a completely new report to test, using the most basic of settings: index.php

require_once "PayrollToPdf.php";


$PayrollRpt = new PayrollToPdf(
[
    "store"=>"1",
    "employees"=>array("1","51"),
    "mindt"=>"2022-08-27",
    "maxdt"=>"2022-09-09"
]
);

$PayrollRpt->run()->render();

PayrollToPdf.php

require_once "../../../koolreport/pro_5.16.2/koolreport/core/autoload.php";
//use \koolreport\processes\ColumnMeta;
//=====================
//Step 2: Creating Report class
class PayrollToPdf extends \koolreport\KoolReport
{
use \koolreport\export\Exportable;
use \koolreport\amazing\Theme;
    protected function settings()
    {
        return array(
        "dataSources"=>array(
            "payroll"=>array(
                "connectionString"=>"sqlsrv:server=SQL1 ; Database=Operations",
                "username"=>"PHPConfig",
                "password"=>"82$0LwrLmav1PH2tR&Da",
                "charset"=>"SQL_Latin1_General_CP1_CI_AS"
                
            ),
        )
    );
    //===

    }
    protected function setup()
    {
    $this->src('payroll')
        ->query("SELECT top 10 STR_ID,USR_ID FROM SY_TIMCRD_LIN")
        
        ->pipe($this->dataStore('payrollpdf'));
    //===
//        $this->src("payroll")
//        ->pipe($this->dataStore("payrollpdf"));
    }    
}

and finally PayrollToPdf.view.php



<?php
    //use \koolreport\widgets\koolphp\Table;
   // use \koolreport\inputs\POSTBinding;
    //use \koolreport\core\Utility;
    use \koolreport\datagrid\DataTables;
    
   // use \koolreport\clients\Bootstrap;
?>
<div class="report-content">
    <div class="text-center">
        <h1>Table Row Group</h1>
        <p class="lead">
            This example shows how to setup row grouping with Table widgets
        </p>
    </div>

    <?php
   
   DataTables::create(array(
        "dataSource"=>$this->dataStore('payrollpdf'),
                   "columns"=> [

    "STR_ID"=>[
    "label"=>"Store",
    "format"=>"string"],
    "USR_ID"=>[
    "label"=>"User ID",

    ]
]
    ));
  

    ?>
    <style>
        .darker
        {
            background:#ccc;
        }
    </style>
</div>

If I do this using Datatables everything works just fine - columns get names. When I use Table then column names DO NOT work and the errors mentioned above are logged. This happens regardless of weather or not I use the passed-in parameters (they are not used in this example but are passed in). Something is definitely wrong with the Table widget, not sure how to fix it. A little help would be appreciated!

Andrew Guattery commented on Sep 12, 2022

FIXED In Table.php on or about line 127 is this line of code:

if ($this->dataStore->countData() == 0 && count($this->dataStore->meta()["columns"]) == 0) {

This needs to be like this:

if ($this->dataStore->countData() > 0 && count($this->dataStore->meta()["columns"]) == 0) {

Notice the countDat > 0 ----- Having this as == 0 prevents the dataStore meta from being created. The line of code as written is saying "If there is no data and there is no meta["columns"] then create the meta.... which makes no sense. it needs to say "If there IS data and there is no meta["columns"} then create the data" Changing to > 0 fixes the issue.

KoolReport commented on Sep 13, 2022

I see, I have sent your report to dev.team about this issue. Your fix makes sense to me.

Andrew Guattery commented on Jul 21, 2023

This is an update on this solution. There exist conditions where some columns have meta data, and others might not.

In my most recent report, I had one line given from a calculated column with meta data, whereas all other columns were without meta data and needed to go through the foreach loop to get meta data assigned. This error re-occured and I realized this update to the code had worked all year, but there is a more accurate solution.

//if ($this->dataStore->countData() == 0 && count($this->dataStore->meta()["columns"]) == 0) {
//if ($this->dataStore->countData() > 0 && count($this->dataStore->meta()["columns"]) == 0) {
if ($this->dataStore->countData() > count($this->dataStore->meta()["columns"]) == 0) {

Because the calculated column created a column of meta data, the 2nd line tests false. What we really want is the 3rd line, because we want to run the loop whenever there are more columns than there is meta data for columns.

Andrew Guattery commented on Jul 25, 2023

Just coming back again to correct a typo.

if ($this->dataStore->countData() > count($this->dataStore->meta()["columns"]) == 0) {

SHOULD BE

if ($this->dataStore->countData() > count($this->dataStore->meta()["columns"]) ) {

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

Export