我想将变量从一个函数传递给Codeigniter中的另一个函数

时间:2018-09-17 09:36:57

标签: php codeigniter

我想在Codeigniter PHP中将变量从一个函数访问到另一个函数。变量为$fromDate$toDate。我想在名为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 个答案:

答案 0 :(得分:0)

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;
    }

}

答案 1 :(得分:0)

您只想从一个函数访问该变量至同一类的另一个函数,如果我没错,则必须将$fromDatetoDate保存到Flash变量中,然后再访问另一个变量功能类似于下面的代码。

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);

}

现在访问另一个功能,例如。

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

        //other code goes here...
    }