Welcome Guest, Not a member yet? Register   Sign In
Most Appropriate way to Achieve...
#1

[eluser]JamesTaylor[/eluser]
What is the best way to achieve the following?

I'm working on a site for a small chain of pubs / restuarants - 3 in total. The site carries information regarding the main company who own the 3 pubs and also shows information specific to each of the pubs.

Within the layout of the pages there is a common area which displays up coming events across the pubs... there is only ever going to be 3 items displayed... but dependent upon which page is being viewed the 3 items will alter slightly.

For example:

On the 'about us' page which is dealing with the parent company it needs to show the next event at each of the pubs - 3 in total - 1 for pub A, 1 for pub B, 1 for pub C.

But, on the pages which are specific to the individual pub it needs to show the next 3 at that pub - 3 for pub A


What i don't want to do is duplicate the code across multiple controllers so it is tailored to every page as it seems long winded. I am thinking of extending the controllers by creating MY_Controller, then grabbing the next 3 events for each pub and passing it to the page.

I would then create 4 views which could be loaded into the main template dependent upon which page of the site is being viewed. The 4 views would be: 1 for the next event at each pub; 1 showing next 3 events at pub A; 1 showing next 3 events at pub B; 1 showing next 3 events at pub C.

However the problem i have with the above is that i am always going to be executing extra database requests in relation to the info i need - this doesn't seem like good pratice to me! When i show the next 3 events at pub A there will still be database calls being made for the next events at pub B and C but this info won't be used?

It seems to me that if the info to be displayed was always the same breakdown, i.e just the next event at each pub extending the controller would be the way to go as i'd only have to do the code once and it'd be available to each page. But is there an appropriate way to achieve a similar efficence but with the variations taken into account?

Sorry its a bit long winded but i want to learn the correct way of doing things!

Thanks, James
#2

[eluser]heavenquake[/eluser]
Create a model ( http://ellislab.com/codeigniter/user-gui...odels.html ) with some functions for getting data from pubs, and then push them to your views. for example:

Controller:
Code:
class Pub extends Controller {

   function index()
   {
       $data->events = $this->pub_model->get_events(); //just get 3 events
       $this->load->view('pub', $data);
   }

   function show()
   {
       $pub_id = $this->uri->segment(3); // pub/show/2, for example
       $data->events = $this->pub_model->get_events($pub_id); //get 3 events for the given pub
       $this->load->view('pub', $data);
   }

}

Model:
Code:
class Pub_model extends Model {

   function get_events($pub_id = FALSE)
   {
      if($pub_id)
      {
          // get events for the given pub id
      }
      else
      {
          // get events for all three pubs
      }
      return $data // return the array/object you got
   }

}

That's what I would do.
#3

[eluser]JamesTaylor[/eluser]
Thanks heavenquake,

I haven't done exactly as you outlined but your suggested methodology got me moving...

i've created MY_Controller to extend all other controllers:
Code:
class  MY_Controller  extends  Controller  {

    var $data = array();

    function MY_Controller ()
    {
        parent::Controller();
        
        
    $pub_id = $this->uri->segment(2);
    $this->load->model('events_model');
    if (!$pub_id)
        {
            $this->data['NextEvents_Stan'] = $this->events_model->GetNext1('events_stan');
            $this->data['NextEvents_Chevin'] = $this->events_model->GetNext1('events_chevin');
            $this->data['NextEvents_Black'] = $this->events_model->GetNext1('events_black');
        }
    elseif ($pub_id === 'stansfield_arms')
        {
            $this->data['NextEvents_Stan'] = $this->events_model->GetNext3('events_stan');
        }
    elseif ($pub_id === 'chevin_inn')
        {
            $this->data['NextEvents_Chevin'] = $this->events_model->GetNext3('events_chevin');
        }
    elseif ($pub_id === 'black_horse')
        {
            $this->data['NextEvents_Black'] = $this->events_model->GetNext3('events_black');
        }
    
    }
}

my model is then
Code:
<?php

class events_model extends Model {
    
    function GetNext1($DB)
    {    
        $this->db->where('Date >=', date("Y-m-d"));
        $this->db->order_by('Date' , 'asc');
        $this->db->limit(1, 0);
        $Results = $this->db->get($DB);
        
        return $Results->result_array();
    }
    
    function GetNext3($DB)
    {    
        $this->db->where('Date >=', date("Y-m-d"));
        $this->db->order_by('Date' , 'asc');
        $this->db->limit(3, 0);
        $Results = $this->db->get($DB);
        
        return $Results->result_array();
    }

}

and then i have created a view which i will load into the templete on every page
Code:
<?php if (isset($NextEvents_Stan))
    {
    foreach($NextEvents_Stan as $row): ?>
    <p class="Events Title">&lt;?php echo htmlspecialchars($row['Title']) ?&gt; @ The Stansfield Arms</p>
    <p class="Events">&lt;?php echo htmlspecialchars(date("D jS F", strtotime($row['Date']))); ?&gt;</p>
    <p class="Events">&lt;?php echo htmlspecialchars($row['Desc']) ?&gt;... &lt;?php echo anchor(''.$row['ID'], 'Read More');  ?&gt;</p>
    <br />
&lt;?php endforeach; }?&gt;
    
&lt;?php if (isset($NextEvents_Chevin))
    {
    foreach($NextEvents_Chevin as $row): ?&gt;
    <p class="Events Title">&lt;?php echo htmlspecialchars($row['Title']) ?&gt; @ The Chevin Inn</p>
    <p class="Events">&lt;?php echo htmlspecialchars(date("D jS F", strtotime($row['Date']))); ?&gt;</p>
    <p class="Events">&lt;?php echo htmlspecialchars($row['Desc']) ?&gt;... &lt;?php echo anchor(''.$row['ID'], 'Read More');  ?&gt;</p>
    <br />
&lt;?php endforeach; } ?&gt;
    
&lt;?php if (isset($NextEvents_Black))
    {
    foreach($NextEvents_Black as $row): ?&gt;
    <p class="Events Title">&lt;?php echo htmlspecialchars($row['Title']) ?&gt; @ The Black Horse</p>
    <p class="Events">&lt;?php echo htmlspecialchars(date("D jS F", strtotime($row['Date']))); ?&gt;</p>
    <p class="Events">&lt;?php echo htmlspecialchars($row['Desc']) ?&gt;... &lt;?php echo anchor(''.$row['ID'], 'Read More', 'class="ReadMore"');  ?&gt;</p>
    <br />
&lt;?php endforeach; } ?&gt;

This seems to work as i want, although i think i will end up having to explictly say that if $pub_id doesn't equal any of the names do the following rather than just having it as if not set because eventuall i imagine that uri segment 2 is going to be needed else where also.

Thanks

James




Theme © iAndrew 2016 - Forum software by © MyBB