CodeIgniter Forums
data passed to view - using objects rather then arrays - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: data passed to view - using objects rather then arrays (/showthread.php?tid=44008)



data passed to view - using objects rather then arrays - El Forum - 07-30-2011

[eluser]CI2RULZ[/eluser]
passing data

$data = array(
'title' => 'My Title',
'heading' => 'My Heading',
'message' => 'My Message'
);

$this->load->view('blogview', $data);

And here's an example using an object:

$data = new Someclass();
$this->load->view('blogview', $data);


can someone post an example of a controller file with Someclass being used to define the title, heading, and message? the example above is provided in documentation, but without context.


data passed to view - using objects rather then arrays - El Forum - 07-30-2011

[eluser]fesweb[/eluser]
You can use a standard object like this:
Code:
$data = new stdClass();
$data->title = 'My Title';
$data->heading = 'My Heading';
$data->message = 'My Message';

$this->load->view(‘blogview’, $data);
You can also, of course, use database result objects as well.
Code:
$this->db->where('id', $id);
$q = $this->db->get('blog_items');
$data = $q->result():
// or even add more data to the object first
$data->my_other_info = 'other info'
$this->load->view('blogview', $data);