Welcome Guest, Not a member yet? Register   Sign In
How to create sidebar widgets/modules i.e. embedding the contents of another controller in a view
#1

[eluser]design_shuffle[/eluser]
Hi,

I have been tearing my hair out over this for about 3 hours, please can someone help?

Here is what I want to achieve...

On my home page I wish to have a sidebar widget/module which displays the latest 10 events from the events controller.

I started off trying to use the widget plugin, but could not get my head around it -Plugin widget

Next I read about extending the standard controller to MY_Controller, so in the MY_Controller I added the code from my events controller which retrieves the latest events using the events model, I presume this is the correct place to put this?

##contents of MY_Controller

Code:
function home_events()
   {
      
           // load library
        $this->load->library(array('table','validation','template'));
        
        // load helper
        $this->load->helper('url', 'form');
        
        // load model
        $this->load->model('event_model','',TRUE);
        $this->load->model('location_model','',TRUE);
        
       $data['base'] = $this->config->item('base_url');
        // offset
        $uri_segment = 3;
        $offset = $this->uri->segment($uri_segment);
        
        // load data
        $events = $this->event_model->get_paged_list($this->limit, $offset)->result();
        
        // generate pagination
        $this->load->library('pagination');
        $config['base_url'] = site_url('event/index/');
         $config['total_rows'] = $this->event_model->count_all();
         $config['per_page'] = $this->limit;
        $config['uri_segment'] = $uri_segment;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        

        // generate table data
        $this->load->library('table');
        $this->table->set_empty(" ");
        $this->table->set_heading('Name', 'Location', 'Date', 'Image', 'Actions');
        $i = 0 + $offset;
        foreach ($events as $event){
            $this->table->add_row($event->name, $event->location, $event->date, $event->image_url,
                anchor('event/view/'.$event->id,'view',array('class'=>'view'))
            );
        }
        $data['table'] = $this->table->generate();
        
     $data['home_events_view']    = $this->load->view('home_events', $tmp_data, TRUE);
     $this->load->view('home', $data);
        }
    //end function home_events

Next in my home view I try and load the home_events_view with the following -
Code:
<?php echo $home_events_view; ?>

When I load the homepage I get the following error -
Message: Undefined variable: home_events_view
Filename: views/home.php

Have I approached this the correct way?, can anyone spot where I have gone wrong?
Im guessing that I need to put something in my home controller to retrieve the events data?

Any help would be appreciated, I have learnt so much lately about codeigniter and I love it, but embedding views has been a big snag!

Thanks Dan
#2

[eluser]slowgary[/eluser]
It seems fairly straightforward, unless I'm missing something...
Code:
function home()
{
     $data['events'] = $this->event_model->get_top ten();
     $data['content'] = $this->some_other_model->get_something();

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

Then in page.php...
Code:
<div id='content'>
&lt;?php echo $content; ?&gt;
</div>
<div id='events'>
&lt;?php foreach($events as $event): ?&gt;
     &lt;?php echo $event; ?&gt;
&lt;?php endforeach; ?&gt;
</div>
#3

[eluser]KingSkippus[/eluser]
I don't know if this is a best practice (or directly conflicts with a best practice...), but what I would typically do is have a separate widget view, and load it in the controller before the rest of the content. So my controller functions usually end up looking something like:

Code:
public function index() {
    // Initialize variables that need to be passed to the views, such as
    // $page_title, $user_info, etc.
    $this->load->view('header', array('title' => $page_title));
    $this->load->view('navbar', array('user_info' => $user_info));
    $this->load->view('index');
    $this->load->view('footer');
}
#4

[eluser]lexusgs430[/eluser]
It sounds like what you want is modularity, check this library out.

http://codeigniter.com/wiki/Modular_Extensions_-_HMVC/

And that is one hell of a controller, from what Ive come to understand about MVC (ive only been really figuring it out for past 2 months so correct me if im wrong smart ppl), you almost always want to try and use skinny controller, fat model/view design.

Think of the controller as the guy with the glowing sticks, pointing to the pilot of the plane to guide him. The traffic control tower is the model, plane/pilot are the view. Glowing stick guy has a very important job, without him, the plane will not line up on the runway in the exact right direction, and everyone on board could die. But, if the glowy stick guy were given additional instructions from either the plane or the control tower beyond "hold these glowy sticks and guide the plane for takeoff", he probably wouldnt know what the hell they were talking about, because they would be using fancy flying terms that he doesen't understand. I think the same is the concept of MVC, and ive noticed the smaller my controllers are, and the less touching the data they do directly, the more reusable and efficient, and understandable things become.
#5

[eluser]design_shuffle[/eluser]
[quote author="slowgary" date="1279766503"]It seems fairly straightforward, unless I'm missing something...
Code:
function home()
{
     $data['events'] = $this->event_model->get_top ten();
     $data['content'] = $this->some_other_model->get_something();

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

Then in page.php...
Code:
<div id='content'>
&lt;?php echo $content; ?&gt;
</div>
<div id='events'>
&lt;?php foreach($events as $event): ?&gt;
     &lt;?php echo $event; ?&gt;
&lt;?php endforeach; ?&gt;
</div>
[/quote]

Thanks, this worked! It looks like I was trying to over complicate things!
#6

[eluser]insub2[/eluser]
...sorry to resurrect a "solved" thread but...

I was wondering if you wanted the events on every page, using this solution you'd need to put those lines of code in every method of every controller? Or is there is another, better, leaner way to do that?
#7

[eluser]insub2[/eluser]
...nevermind. I found it: Creating a sidebar without duplicate code.




Theme © iAndrew 2016 - Forum software by © MyBB