如何将变量从1个函数传递给另一个函数

时间:2015-07-09 12:48:18

标签: php codeigniter

我想将变量从codeigniter中的一个函数传递给另一个共享同一个类的函数。他们是形式的一部分。

class C_Reservation extends CI_Controller {

 private $params = array();
 private $post;
 public function index()
     {
          $this->params['post'] = array(
                'name' => '',
                'last_name'=>''
          );

          $this->load->view('common/default_header',$this->params);
          $this->load->view('reservation/index',$this->params);
          $this->load->view('common/default_footer');

     }

        public function add(){
            $error =false;
            $error_list =array();

            session_start();
            $this->load->library('InputProcess');
            $this->load->library('session');

            if($this->input->server('REQUEST_METHOD')!="POST"){
                show_404("Formularz dodawania obiektu - brak danych POST");
            }

            //filtrowanie danych

            $this->post=array(
                'name'=>$this->input->post('name'),
                'last_name'=>$this->input->post('last_name')
            );
            echo $this->post['name']; // this place is showing name from form
            $this->load->view('common/default_header',$this->params);
            $this->load->view('reservation/complete',$this->params);
            $this->load->view('common/default_footer');

        }

     public function export_pdf(){
         session_start();

         $html=$this->load->view('pdf/index', $this->post,false);
         $this->load->library('m_pdf');
        $pdf = $this->m_pdf->load();
        $pdf->WriteHTML($html);
        $pdf->Output();
     }
}

添加函数未完成它只需要“保存”我可以从函数export_pdf访问它的变量 有谁?

2 个答案:

答案 0 :(得分:0)

如果您没有同时调用这两个函数,则必须在cookie / session中存储值,然后只有一个可以从一个方法获取值到另一个方法。记住HTTP是无状态状态。

其他明智的简单$ this-> export_pdf($ f_name,$ l_name); 使用函数export_pdf($ f_name,$ l_name){....}检索它 或者在你的情况下$ this-> export_pdf();和函数export_pdf(){$ f_name = $ this-> post ['f_name']; $ l_name = $这 - >交[ 'l_name'];}

答案 1 :(得分:0)

你的问题有点令人困惑。原因令人困惑是因为您没有解释应用程序如何流动的过程。考虑到所有评论,我相信流程是这样的:

当用户向系统添加预订时:

  • 将POST请求发送到add()函数
  • 该功能确保它是POST请求
  • 该函数将POST值设置为类变量($ this-> post)
  • 该函数检查用户是否需要PDF输出
  • 如果是,则应运行export_pdf()以创建pdf
  • 如果不是,则应返回已完成的html

如果以上是您想要的流量,则问题在于您的add()export_pdf()功能。该函数需要确保调用export_pdf(),并且export_pdf()需要返回数据,但目前没有任何函数正在运行该函数。

我会重写这两个函数:

public function add(){
    $error = false;
    $error_list = array();

    session_start();
    $this->load->library('InputProcess');
    $this->load->library('session');

    if($this->input->server('REQUEST_METHOD')!="POST"){
        show_404("Formularz dodawania obiektu - brak danych POST");
    }

    //filtrowanie danych

    $this->post = $this->input->post();
    if ($this->post['user_needs_pdf'] == TRUE) {
        $this->export_pdf();
    } else {
        $this->load->view('common/default_header',$this->params);
        $this->load->view('reservation/complete',$this->params);
        $this->load->view('common/default_footer');
    }
}

public function export_pdf(){
    $html = $this->load->view('pdf/index', $this->post, false);
    $this->load->library('m_pdf');
    $pdf = $this->m_pdf->load();
    $pdf->WriteHTML($html);
    $pdf->Output();
 }
相关问题