KoolReport's Forum

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

Problem with $_SESSION #292

Open arthur opened this topic on on May 22, 2018 - 10 comments

arthur commented on May 22, 2018

Hi,

I want to use a custom function, with a $_session variable inside. The problem is : the $_SESSION variable seems to be undefined inside this function, whereas it works outside this function. When I use the $_SESSION with Filter it works fine.

here is my code :

$this->src('sales')


        ->pipe(new Filter(array(
            array("Ccial","=",$_SESSION['Username']),
            'or',
            array("ACC","=",$_SESSION['Username']),
        )))

->pipe(new Map(array(
      '{value}' => function($row, $metaData) {
        if ($row['Société'] != null && trim($row['Société']) != '' && trim($row['Société']) != '-')
          return array($row);
      },
    )))


->pipe(new Filter(array(
         array("Ccial","!=",null),
            array("Société","!=",null),
 )))


->pipe(new CalculatedColumn(array(
            "PO"=>function($data){

                        return  ($data["Marge_FAS_Déclarée"] + 12 * $data["MARGE_DELTA_REC"]); 
                   
            }
        )))

->pipe(new CalculatedColumn(array(
            "Challenge"=>function($data){	

              if ($_SESSION['Username'] !='VALIFAX' && $data["ACC"]!='VALIFAX' ){

                    if ($data["Prospect_Client"]=='client' ){
                        return  ( ($data["Marge_FAS_Déclarée"] + 12 * $data["MARGE_DELTA_REC"])*1.1 ); 
                    }
                    else { 
                        return ( ($data["Marge_FAS_Déclarée"] + 12 * $data["MARGE_DELTA_REC"]) ) ; 

                    }

               } 
               else {
                
                    if ($data["Prospect_Client"]=='client' ){
                        return  ( 2.5 * ($data["Marge_FAS_Déclarée"] + 12 * $data["MARGE_DELTA_REC"])*1.1 ); 
                    }
                    else { 
                        return ( 2.5 * ($data["Marge_FAS_Déclarée"] + 12 * $data["MARGE_DELTA_REC"]) ) ; 
                    }
               }
            }   
        )))

        ->pipe(new Filter($filter_args))

        
        ->pipe($this->dataStore('commandes'));
KoolReport commented on May 22, 2018

I will do some testing session in function and come back you.

David Winterburn commented on May 22, 2018

Hi Arthur,

Can you please let us know the $_SESSION variable in which line of code did not work in the above code? Thanks!

arthur commented on May 22, 2018

Yes, it's this line

if ($_SESSION['Username'] !='VALIFAX' && $data["ACC"]!='VALIFAX' )
David Winterburn commented on May 22, 2018

How did $_SESSION not work? Would you please echo $_SESSION['Username'] and confirm its value just before the if. Thanks!

arthur commented on May 22, 2018

it echoes VALIFAX

David Winterburn commented on May 22, 2018

Then what is the issue with $_SESSION here, could you explain specifically? Thanks!

arthur commented on May 22, 2018

Ok, so with the code below, it echoes 1 when I log with some other Id than VALIFAX ` if ($_SESSION['Username'] ='VALIFAX' && $data["ACC"]='VALIFAX' ){ echo $_SESSION['Username'];

               if ($data["Prospect_Client"]=='client' ){
                    return  ( 2.5 * ($data["Marge_FAS_Déclarée"] + 12 * $data["MARGE_DELTA_REC"])*1.1 ); 
                }
                else { 
                    return ( 2.5 * ($data["Marge_FAS_Déclarée"] + 12 * $data["MARGE_DELTA_REC"]) ) ; 
                }

           } 
           else {
           	   	echo $_SESSION['Username'];
            
                  if ($data["Prospect_Client"]=='client' ){
                    return  ( ($data["Marge_FAS_Déclarée"] + 12 * $data["MARGE_DELTA_REC"])*1.1 ); 
                }
                else { 
                    return ( ($data["Marge_FAS_Déclarée"] + 12 * $data["MARGE_DELTA_REC"]) ) ; 

                }
           }

KoolReport commented on May 22, 2018

Thats right. Because you use “=“ means [assign value], not the double “==“ means [compare].

David Winterburn commented on May 22, 2018

Please change your above code:

if ($_SESSION['Username']='VALIFAX' ){

to:

if ($_SESSION['Username']=='VALIFAX' ){

In PHP and most languages, one equal sign is actually an assignment which always returns true. When making comparison you would want double or tripple equal sign. Please let us know if this works for you. Thanks!

arthur commented on May 22, 2018

It works !!! Thanks a lot

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