Welcome Guest, Not a member yet? Register   Sign In
Default Layout
#11

[eluser]derekmichaeljohnson[/eluser]
[quote author="Dam1an" date="1242597560"]If that doesn’t solve it, you’ll have to pass the data array in another array, and then pass the sub data array (the real data) into the second level view
I hope that makes sense[/quote]It probably does, but I'm too new to CI to get it :-)

Can you give me an example of how I can pass the data array from the index into an array and then load it into the second level view?

Thanks man.
#12

[eluser]Dam1an[/eluser]
I just tested it using load->vars and it works
Code:
// index function
function index() {
    // Add something to the MY_Controller data array
    $this->data['cal'] = $this->calendar->generate()
    
    // Do other stuff here
    // Remember, no need to call render manually
}

// MY_Controller - render
// Keep the same constructor as you had before
function render() {
    // Load the data for the views
    $this->load->vars($this->data);
    // Load the view, no longer need to pass the data array
    $this->load->view('layout');
}

Thats all there is too it
#13

[eluser]derekmichaeljohnson[/eluser]
But this:
Code:
$this->data['cal'] = $this->calendar->generate()
won't be coming from the MY_Controller class, it will be coming from the application controller classes (home, blog, news, etc).

So if the render method is being run post-controller, how do I grab the data array from the "Home" controller class (for example), collect it in the render method and pass it with the layout view, within which the controller-specific view ("home/home_view" in this example) is loaded?

Haha it sounds pretty damn complicated for what you would think would be an easy task.
#14

[eluser]Dam1an[/eluser]
Because you've already declared the variable $data in MY_Controller
So when you call $this->data, as it already exists in that classes (you home controllers) scope, it will use that
So when your post controller executed, it goes to the MY_Controller, and all the data in in that data array Smile
#15

[eluser]mighty_falcon[/eluser]
hey there,

I was just attempting the same thing as laid out by your sample code but for some reason I am not able to load up any data using $this->data = array('whatever' => 'whatever) which is being set in the particular controller (in this case the home one).

Any ideas?
#16

[eluser]Dam1an[/eluser]
Can you post some code so I can get a better idea of what you mean?

The only things I could suggest off the top of my head is to make sure you decare the data variable in your class and then make it into an array
Code:
$data = array();

Not having that could cause some issues if you're running PHP4 I think (at least someone had that problem in the past and that was the cause)
#17

[eluser]mighty_falcon[/eluser]
[quote author="Dam1an" date="1244553275"]Can you post some code so I can get a better idea of what you mean?

The only things I could suggest off the top of my head is to make sure you decare the data variable in your class and then make it into an array
Code:
$data = array();

Not having that could cause some issues if you're running PHP4 I think (at least someone had that problem in the past and that was the cause)[/quote]

well my classes are exactly like the ones you posted in your example:

MY_Controller
Code:
class MY_Controller extends Controller {
    protected $data;
    protected $title;
    protected $view;
    
    
    
    /**
     * MY_Controller::__construct()
     *
     * Used to simply set-up the variables to be used when rendering the template.
     */
    public function __construct() {
        parent::Controller();
        // Initialise the default views
        //$this->views['menu'] = 'template/menu';
        //$this->views['sidebar'] = 'template/sidebar';
        //$this->views['main'] = 'template/main';
        //$this->views['footer'] = 'template/footer';
        
        // theme files go under controller/method
        $uri = $this->router->class.'/'.$this->router->fetch_method();
          $this->view = $uri;
          
          //sets the default title, each method should override this
          $this->title = '....title....';
  
    }
    
    /**
     * MY_Controller::render()
     *
     * @return: The default view for the main theme.
     *             Each subsequent view is then loaded via creating a folder and .php file in the view folder such as
     *
     *             CONTROLLER/METHOD.php
     */
    function render() {
        // We need the views and the data in the master template
        $this->load->view('main', array('data'=>$this->data, 'view'=>$this->view, 'title' => $this->title));
    }

the conroller file:
Code:
//helpers
        $this->load->helper('form');
        $this->load->helper('url');
        
        $popular_query = $this->db->query("SELECT term, COUNT(sid) AS total FROM searches GROUP BY term ORDER BY total DESC");
    

        $popular = $popular_query->result_array();
        
        $this->data = $popular;
        $this->title = 'test';

I cannot seem to be able to access any data i pass on to $this->data from the controller class...am i doing something wrong when extending MY_controller?
#18

[eluser]mighty_falcon[/eluser]
you know the strange thing is that if i do a print_r($this->data) on the application controllers (home in this case) it accesses the indices set in the MY_Controller constructor just fine. If i were to do print_r($this->data) in the render function i do not get any indices that i created in the application controller. Have you gotten this to work with the post_controller hook yourself?
#19

[eluser]jedd[/eluser]
Hmm .. a few things spring out there (though I'm having trouble identifying your intent in your MY_Controller)

In your controller, do you extend MY_Controller, or just Controller?

Why - in your controller - are you assigning:
Code:
$this->data = $popular;

This will overwrite any extant data in $this->data. You would instead need to do something like this:
Code:
$this->data['popular'] = $popular;
This means that in your view, later, you'll have a $popular variable to play with. And every young variable wants to be popular.

Most importantly I think is your render() method in your MY_Controller - I do not think it does what you think it will do.

Read up in the [url="http://ellislab.com/codeigniter/user-guide/general/views.html"]CI manual on generating views[/url] - specifically the third parameter being set to TRUE - and then look at your render function again, and probably assign the output of that view generation to a variable, or just return it (I'd go for the former, and test for success/fail before returning anything - but I'm a cautious kind of guy).
#20

[eluser]Dam1an[/eluser]
Jedd seems to have picked up on the main points (as always)
I would just like to say (disclaimer) this was never meant to be widely used code, it was just a 'hack' I posted to help a user... I've since moved to something more complex, and moved it into a seperate library.

As Jedd mentioned, you're overwritting the data array, so you may expect to have a veriable $popular in the view, but instead get $0, $1, $2 etc (Is it even possible to have a variable start with a number?)

As for the view issue, I was loading a master view which would load nested views in it, you seem to be loading is as a single view, so the top level view only has the arrays (such as data, views) not their contents as you expect




Theme © iAndrew 2016 - Forum software by © MyBB