Codeigniter: Passing data from controller to view

后端 未结 13 2047
忘了有多久
忘了有多久 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:27

    You just need to create a array, you using codeigniter right?

    Example on controller:

    $data['hello'] = "Hello, world";
    $this->load->view('results_view', $data);
    

    In de page "results_view" you just have to:

    <?php echo $hello;?>
    

    Obs: You can create n datas, just pay attention in the name and make it a array.

    Obs²: To use the data use the key of the array with a echo.

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

    you can do it this way

    defined array in controller

    $data['hello'] = "hello";
    

    and pass variable to view

    echo $hello; 
    
    0 讨论(0)
  • 2020-11-27 07:29

    $data should be an array or an object: http://codeigniter.com/user_guide/general/views.html

    $data = array(
        'title' => 'My Title',
        'heading' => 'My Heading',
        'message' => 'My Message'
    );
    
    $this->load->view('results_view', $data);
    

    results_view.php

    <html>
    <?php 
    //Access them like so
    echo $title.$heading.$message; ?>
    </html>
    
    0 讨论(0)
  • 2020-11-27 07:31

    In controller:

    $data["result"] = $this->login_model->get_login(); // Get array value from DB..
    
    $this->load->view('login-form',$data); // Pass the array to view 
    

    In view:

    print_r($result);  // print the array in view file
    
    0 讨论(0)
  • 2020-11-27 07:35

    Instead of

    $data = "hello";
    $this->load->view('results_view', $data);
    

    Do

    $data['hello'] = 'hello';
    $this->load->view('results_view', $data);
    

    In your controller file and controller will send data having hello as string to results_view and in your view file you can simply access by

    echo $hello;
    
    0 讨论(0)
  • 2020-11-27 07:36

    In controller:

    public function product(){

    $data = array("title" => "Books", "status"=>"Read","author":"arshad","company":"3esofttech",
    

    "subject":"computer science");

    Data From Model to controller

    $this->load->model('bookModel');
    $result = $this->bookModel->getMoreDetailsOfBook();
    
    **Add *$result* from model to *$data* array**  
    $data['tableRows'] = $result;
    

    $data from Controller to View

    $this->load->view('admin/head',$data);
    

    And to access in view file views/user.php

    <?php  echo $data;
     foreach($tableRows as $row){ echo
     $row['startData']; } ?>
    
    0 讨论(0)
提交回复
热议问题