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 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口