Welcome Guest, Not a member yet? Register   Sign In
Undefined Variable when using Views
#1

[eluser]Brandon Beasley[/eluser]
Home Controller

Code:
<?php

class Home extends Controller {

    function Home(){
    
        parent::Controller();    
    }
    
    public function index()
    {
        
        $data = new stdClass();  // WORKING
        
              $data->trends = $this->twitter->search('trends'); // WORKING
        
        
        // print_r($data);
        
        $this->load->view('trends_view', $data);

The results of print_r when executed on Home is displaying the object, but the View is not getting the $data variable passed to it.

trends_view View
Code:
<?php print_r($data); ?>

This is what is returned:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: views/trends_view.php


This is my first post, go easy on me Smile
#2

[eluser]jwindhorst[/eluser]
As far as I know, $data should be an array not an object. Try
Code:
$data['trends'] = $this->twitter->search('trends');
#3

[eluser]Brandon Beasley[/eluser]
I changed to:

Code:
$data['trends'] = $this->twitter->search('trends');
        
$this->load->view('trends_view', $data);

Still getting error when accessing view:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: views/trends_view.php



The $data variable is accessible and contains the correct data when viewed within Home controller.
#4

[eluser]JHackamack[/eluser]
When passing an array to a view it strips down a layer.

Example;

$data['trends'] in controller becomes $trends in the view.
#5

[eluser]Brandon Beasley[/eluser]
Seems to be working now...

I pass the $data variable into the view and then I reference the $trend variable once in the view.

This was most likely simple, I just made it difficult.

Thanks for the help guys!




Theme © iAndrew 2016 - Forum software by © MyBB