KoolReport's Forum

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

If Condition Numeric Comparison not working in Custom Process #2354

Open hsw opened this topic on on Sep 24, 2021 - 3 comments

hsw commented on Sep 24, 2021

Hi,

I can't seem to figure out why the following if condition with numeric comparison is not working. Can someone help? Thank you.

    protected function setup()
    {       
        $pamt = $this->params["pamt"];

        $this->src('packages')
        ->pipe(new ColumnMeta(array(
            "min_amt"=>array("type"=>"number")
        )))
	->pipe(new Custom(function($row){
                  if ($row("min_amt") <= $pamt) {
                            $row["remark"] = "Eligible for subsidy";   }
        }

KoolReport commented on Sep 24, 2021

Because you are using round brackets ( ), you should use rectangle brackets [ ] for array. Hope that helps.

Sebastian Morales commented on Sep 27, 2021

I think one of the problem is that you used an outside variable in your custom anonymous function. To use it you must use the "use" keyword:

	->pipe(new Custom(function($row) use ($pamt) { //add use ($pamt) here
                  if ($row["min_amt"] <= $pamt) {
                            $row["remark"] = "Eligible for subsidy";   }
        }
hsw commented on Sep 27, 2021

Hi,

Thanks for the replies.

Yes, the problem is outside variable seems to be ignored and the condition is always not satisfied. It works after I follow Sebastian's example.

It would be helpful to beginners if the use of external variables in the processes is mentioned in the Documentation with examples.

The bracket is a typo-error when I re-created the sample codes to this forum. Sorry about that.

Thank you very much for all the help.

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
solved

None