Codeigniter: Passing data from controller to view

后端 未结 13 2045
忘了有多久
忘了有多久 2020-11-27 06:57

I want to pass $data from the controller named poll to the results_view however I am getting an undefined variable error.

         


        
相关标签:
13条回答
  • 2020-11-27 07:14

    If you pass

    $data = your code
    $this->load->view('your-page', $data);
    

    and get data on your view as

    <?php echo $data;?>
    

    It won't work because ci didn't understand this patern. If like to pass value form controller to view so you can try this -

    controller -

    $data['any-name'] = your values;
    $this->load->view('your-page', $data);
    

    then in your view you can get this data by -

    <?php echo $any-name;?>
    

    Hope this helps you.

    0 讨论(0)
  • 2020-11-27 07:16

    In simple terms,

    $data['a'] in controller becomes $a in your view. ($data won't exist in your view, only the index will become available)

    e.g.

    Controller:    
    $data['hello']='hellow world';
    
    view:
    echo $hello;
    
    0 讨论(0)
  • 2020-11-27 07:16

    The view wouldn't call the data 'data'

    The controller would include an associative index (not sure if that's correct nomenclature) for data e.g 'stuff' looking thus $data['stuff']

    You'd echo in the view so: echo $stuff; not echo $data;

    I am a v lowly code fiddler but do really like CodeIgniter so excuse me if i've got this arse about tit.

    One more thing - surely your constructor function is a bit of a waste. All that loading of libraries and helpers is done with the autoload file.

    0 讨论(0)
  • 2020-11-27 07:16

    In your controller you can pass

    $data['poll'] = "Your results";
    

    In your view you can call

    echo $poll; 
    
    0 讨论(0)
  • 2020-11-27 07:22

    I have seen all above answer so here is what I do when I have to load the data from the controller to my view. To Pass the Data To the view from the controller:

    public function your_controller(){
    
       // Your Necessary Code 
       // You have the $data, $data2, $data3 to post to the view.
    
       $this->load->view('your_view_directory or view_page',['data'=>$data, 'data2'=>$data2, 'data3'=>$data3... so on ]);
    
    }
    

    And At the View Side You can simply retrieve that data: To Display You can simply use echo, print, print_r. And if you want to loop over it, you can do that as well.

    0 讨论(0)
  • 2020-11-27 07:23

    Ok so I finally solved it. You should really have a model (it helps a lot)

    In your model do something like

    Model

    class poll_model extends CI_MODEL {
    
     function __construct() {
       $this-load->database(); 
     }
    
     function get_poll {
       $this->db->query("SELECT * FROM table");
       $row = $query->row();
    
       $obj = array(
        'id' => $row->id
      );
       return $obj;
     }
    }
    

    Now if you have more than an id say name of poll # you can add in array. Now in your controller do

    class Poll extends CI_Controller {
    
    public function __construct()
       {
            parent::__construct();
            $this->load->database();
            $this->load->helper('form');
            $this->load->model('poll_model');
       }
    
    public function index()
    {
        $data["a"] = $this->poll_model->get_poll();
        $this->load->view('poll_view',$data);
    }
    

    And finally in VIEW put

    <? echo $a["id"]; ?>
    

    This is a big help. I figured it out by testing and it works for me.

    0 讨论(0)
提交回复
热议问题