CodeIgniter Forums
Am I Loading this View wrong? - 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: Am I Loading this View wrong? (/showthread.php?tid=54004)



Am I Loading this View wrong? - El Forum - 08-17-2012

[eluser]Kraig[/eluser]
This is my errior - Message: Undefined variable: user_info
On my home page I have a Views template that looks like this:
Code:
<?php $this->load->view('includes/header'); ?>

<?php $this->load->view('includes/topMembers'); ?>

<?php $this->load->view('includes/news'); ?>

<?php $this->load->view($main_content); ?>

<?php $this->load->view('includes/footer'); ?>

Now I'm trying to figure out how to create a topMembers controller that way I can load a model and get some topMembers information. Keep in mind that the topMember view is loaded onto my home page. I do have a topMembers controller, but for some reason when I "reload" the topMembers view with a variable it is not working. Am I going about this wrong? Here's my topMembers controller
Code:
class TopMembers extends CI_Controller {

function index()
{
  // Grabbing all the files associated with the current user
  $this->load->model("membership_model");
  $q = $this->membership_model->user_info();
  $data['user_info'] = $q;
  
  $this->load->view('includes/topMembers', $data);

}

}

And here is my topMembers View
Code:
<?php
$user = array();
foreach ($user_info as $value)
{
  $user[] = $value['first_name'];
}
?>

//HTML below



Am I Loading this View wrong? - El Forum - 08-17-2012

[eluser]CroNiX[/eluser]
You're loading the topMembers view 2x, and the first time you're not sending data to it so $user_info comes up undefined.


Am I Loading this View wrong? - El Forum - 08-17-2012

[eluser]Kraig[/eluser]
So which load do I need to do? I want topMembers to be loaded onto the home page, and home page loads that template. Do I need to move my query over to the home pages controller in order to use it?


Am I Loading this View wrong? - El Forum - 08-17-2012

[eluser]Kraig[/eluser]
I did it through my home pages controller...seeing how that's the page that is loaded, and topMembers a subview. It works now, but is there a way of doing it the other way?


Am I Loading this View wrong? - El Forum - 08-17-2012

[eluser]Oscar Dias[/eluser]
If your views inside /include aren't going to change, you can create a library that loads that for you. Something like this:
Code:
class Common {
    function show($view, $data = array())
    {
        // Get current CI Instance
        $CI = & get_instance();
                
        // Grabbing all the files associated with the current user
        $CI->load->model("membership_model");
        $q = $CI->membership_model->user_info();
        $data_topMembers['user_info'] = $q;

        // Load include views
        $CI->load->view('includes/header');
        $CI->load->view('includes/topMembers', $data_topMembers);
        $CI->load->view('includes/news');
        $CI->load->view($view, $data);
        $CI->load->view('includes/footer');
    }
}

Depending on your needs this might be a good solution. You can pass additional parameters, for example SEO information for your header view... Then in your controller you do:
Code:
$this->load->library('common');
$this->common->show($main_content);