【问题标题】:Can i pass data between functions in codeigniter?我可以在codeigniter中的函数之间传递数据吗?
【发布时间】:2017-07-21 15:03:13
【问题描述】:

我正在加载一个简单的表单作为子视图,我需要将数据传递给下一个函数,但是当我使用$this->data时,我得到一个空白页。这是我的控制器:

 <?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Search extends Admin_Controller {


    function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->helper('form');
        $this->load->model('search_model');

    }
    function index()
    {

        $this->data['subview'] = ('../views/user/search_form');
        $this->load->view('../views/_layout_admin', $this->data);
    }
    function execute_search()
    {
        // Retrieve the posted search term.
        $search_term = $this->input->post('search');

        // Use a model to retrieve the results.
        $data['results'] = $this->search_model->get_results($search_term);

        // Pass the results to the view.
        $this->data['subview'] = ('../views/user/search_results');
        $this->load->view('../views/user/search_results', $this->data);

    }
    }

任何帮助/想法?非常感谢

【问题讨论】:

  • 嗨,我认为您需要阅读所有 codeigniter 的文档,因为当您在 application/views/... 中加载视图时,您不需要仅使用 $this-&gt;load-&gt;view('../views/--- $this-&gt;load-&gt;view('views/... 然后,当您发送 $data 查看你需要的是$this-&gt;load-&gt;view('../views/view_tamplate', $data); 作为文档
  • 你在用什么 $this->data 为什么不只用 $data?

标签: php forms codeigniter


【解决方案1】:

您也可以在主视图中加载子视图:

<?php $this->load->view('user/search_form'); ?>

【讨论】:

    【解决方案2】:

    用这段代码替换你的控制器:

     <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    class Search extends Admin_Controller {
    
    
        function __construct()
        {
            parent::__construct();
            $this->load->library('form_validation');
            $this->load->helper('form');
            $this->load->model('search_model');
    
        }
        function index()
        {
    
            $data['subview'] = ('../views/user/search_form');
            $this->load->view('layout_admin', $data);
        }
        function execute_search()
        {
            // Retrieve the posted search term.
            $search_term = $this->input->post('search');
    
            // Use a model to retrieve the results.
            $data['results'] = $this->search_model->get_results($search_term);
    
            // Pass the results to the view.
            $data['subview'] = ('../views/user/search_results');
            $this->load->view('search_results', $data);
    
        }
        }
    

    【讨论】:

      猜你喜欢
      • 2021-11-20
      • 1970-01-01
      • 2018-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多