dongsu0308 2015-07-09 12:48
浏览 46
已采纳

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

i want to pass variable from one function in codeigniter to another they are sharing the same class. they are part of form.

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

add function is unfinished it just need to "save" variable that i can acces it from function export_pdf anyone ?

  • 写回答

2条回答 默认 最新

  • dongluan5740 2015-07-09 17:28
    关注

    Your question is a bit confusing. The reason is confusing is because you are not explaining the process of how the application flows. Taking in consideration all the comments, I believe the flow is like this:

    When a user adds a reservation to the system:

    • a POST request is sent to the add() function
    • the function makes sure it is a POST request
    • the function sets the POST values to class variable ($this->post)
    • the function checks if the user wants a PDF output
    • If yes, then should run the export_pdf() to create a pdf
    • If no, then the completed html should be returned

    If the above is your desired flow, then problem lies in your add() and export_pdf() functions. The function needs to make sure the export_pdf() is called, and export_pdf() needs to return the data, but currently nothing is running that function.

    I would rewrite both functions like this:

    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();
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 一道python难题2
  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备