Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter: Passing data from controller to view

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

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');  class Poll extends CI_Controller {      public function __construct()        {             parent::__construct();             $this->load->database();             $this->load->helper('form');        }      public function index()     {          $this->load->view('poll_view',$data);     }      public function vote()     {         echo "Voting Successfull";         $this->db->insert('votes',$_POST);     }      public function results()     {         echo "These are the results";         //$query = $this->db->get('votes');         $data = "hello";         $this->load->view('results_view', $data);      } } 

Results_view.php

<html> <?php echo $data; ?> </html> 
like image 635
Andrew Lynch Avatar asked Feb 25 '12 18:02

Andrew Lynch


People also ask

How pass data from controller view in CodeIgniter using Ajax?

click(function(){ var id_thang = $(this). attr('value'); $. ajax({ url: baseUrl+'/Home/getImage', dataType: 'json', type: 'POST', data: {id_thang: id_thang}, }). done(function(result) { console.

How pass multiple variables from controller view in CodeIgniter?

$data=array($var1,$var2); $this->load->view('myview',$data);

How pass data from one page to another in CodeIgniter?

call first controller from first view and pass form data to second view. On second view you can create hidden inputs and set their values from controller data. Now submit the second form to final controller and you will get all the values of both form. Hope this helps you.

How pass data from controller view in MVC in php?

Since a controller writes either to view or model - you'd pass variables to view via controller. $model = new Model(); $view = new View($model); $controller = new Controller($view); // This will assign variables to view $controller->indexAction(); echo $view->render();


1 Answers

$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> 
like image 130
Lawrence Cherone Avatar answered Sep 23 '22 02:09

Lawrence Cherone