I want to pass variables from one function to another function in codeigniter

Viewed 49

I want to access variables from one function to another function in Codeigniter PHP. The Variables are $fromDate and $toDate. And I Want to access it in function called sales_reports_print.

public function get_sales_reports()
    {
        if ($this->input->post()) {
            $daterange     = $this->input->post('daterange');
            $sales=explode("-",$daterange);
             $fromDate = trim($sales[0]);
            $toDate = trim($sales[1]);

        }
        $where = array(
            'add_date >=' =>$fromDate ,'add_date <='=>$toDate
        );

        $this->data['sales_reports']=$this->Common_model->select_fields_where_like_join("add_sales","*",'',$where);
        $this->show('reports/sales_reports',$this->data);
    }



function sales_reports_print($fromDate,$toDate)
    {
        $where = array(
            'add_date >=' =>$fromDate,'add_date <='=>$toDate
        );

        $this->data['sales_reports']=$this->Common_model->select_fields_where_like_join("add_sales","*",'',$where);
        $this->show('reports/sales_reports_print',$this->data   );
    }
2 Answers
class ABC {

    public $variable1;
    public $variable2;

    public function __construct() {

        parent::__construct();
        $this->variable1 = '';
        $this->variable2 = '';
    }

    function ab($frdate, $todate) {
        $this->variable1 = $frdate;
        $this->variable2=$todate;
    }

    function cd(){
        $fromdate=$this->variable1;
        $todate=$this->variable2;
    }

}

You just want to access the variable from one function to other of the same class, if I'm correct then you have to save the $fromDate and toDate into a flash variable and access into other function like below code.

public function get_sales_reports()
{
    if ($this->input->post()) {
        $daterange     = $this->input->post('daterange');
        $sales=explode("-",$daterange);
        $fromDate = trim($sales[0]);
        $toDate = trim($sales[1]);

    }

    $this->session->set_flashdata('fromDate', $fromDate );
    $this->session->set_flashdata('toDate', $toDate );
    $where = array(
        'add_date >=' =>$fromDate ,'add_date <='=>$toDate
    );

    $this->data['sales_reports']=$this->Common_model->select_fields_where_like_join("add_sales","*",'',$where);
    $this->show('reports/sales_reports',$this->data);

}

And now access this another function like.

public function sales_reports_print()
    {
        $fromDate = $this->session->flashdata('fromDate');
        $toDate = $this->session->flashdata('toDate');

        //other code goes here...
    }
Related