title: Dynamic Pages - Codeigniter
body: <p><strong>Update:</strong></p>
  
  <p>I have tried Sheldon's code below but I have struck a couple of issues:</p>
  
  <ol>
  <li><p>When using the line <code>$this-&gt;load-&gt;view($page-&gt;template, array('content' =&gt; $page-&gt;content), TRUE);</code>Unable to load the requested file .php but if I change it to <code>$page-&gt;content</code> it works fine.</p></li>
  <li><p>When I place the index functions into my other controllers it still loads the default home controller despite what the uri segment is set to.  How do I adjust the model for this to work?</p></li>
  </ol>
  
  <p><strong>Original Question:</strong></p>
  
  <p>I have a template.php file which houses my main site structure. Suppose I have a page called 'home'. I would like to set the <code>$title</code> and the <code>$content</code> data from the database.</p>
  
  <p>I have set a view for each page and also a controller. I have tried:</p>
  
  <pre><code>$this-&gt;cms_pages-&gt;name;
  </code></pre>
  
  <p>This produces nothing. I know I need to get the URI segment but because I am using <code>/$sales-&gt;name</code> I am unsure if I have to change it to <code>$sales-&gt;id</code> for this to work. I would like to get it by the name. Best practices are also important.</p>
  
  <p>Is there a way that I could do something like: <code>$this-&gt;cms_pages-&gt;id-&gt;1-&gt;name</code>?</p>
  
  <p><strong>Model</strong></p>
  
  <pre><code>function getCMSPages() {
  
      $query = $this-&gt;db-&gt;get('pages');
      if($query-&gt;num_rows() &gt; 0) return $query-&gt;result();
  }
  </code></pre>
  
  <p><strong>Template</strong></p>
  
  <pre><code>&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  
  class Home extends CI_Controller {
  
  public function index()
  {
  $data['cms_pages'] = $this-&gt;navigation_model-&gt;getCMSPages();
  $data['title'] = 'Ccho';
  $data['content'] = $this-&gt;load-&gt;view('home', NULL, TRUE);
  $this-&gt;load-&gt;view('template', $data);
  }
  }
  </code></pre>
  
  <p><strong>Template</strong>
      
  
  <pre><code> $stylesheetCSS = array('href' =&gt; 'includes/css/style.css', 'rel' =&gt; 'stylesheet',   'type' =&gt; 'text/css', 'media' =&gt; 'print');
  
  echo doctype('html5');
  ?&gt;
  &lt;head&gt;
   &lt;title&gt;Housemovers : &lt;?php echo $title ?&gt;&lt;/title&gt;
    &lt;?php echo link_tag($stylesheetCSS);?&gt;
     &lt;/head&gt;
   &lt;body&gt;
  &lt;div id ="wrapper"&gt;
  &lt;header&gt;
  &lt;p&gt;content&lt;/p&gt;
  &lt;/header&gt;
  &lt;nav&gt;
      &lt;ul&gt;
      &lt;?php if($this-&gt;session-&gt;userdata('logged_in')): ?&gt;
          &lt;li&gt;&lt;?php echo anchor('#','Edit Pages');?&gt;
          &lt;?php if(is_array($cms_pages)): ?&gt;
                  &lt;ul&gt;
                      &lt;?php foreach($cms_pages as $page): ?&gt;
                      &lt;li&gt;&lt;a href="&lt;?=$page-&gt;title?&gt;"&gt;&lt;?= $page-&gt;title?&gt;&lt;/a&gt;&lt;/li&gt;
                      &lt;?php endforeach; ?&gt;
                  &lt;/ul&gt;
              &lt;?php endif; ?&gt;
              &lt;/li&gt;
          &lt;li&gt;&lt;?php echo anchor('#','Gallery');?&gt;
          &lt;ul&gt;
              &lt;li&gt;&lt;?php echo anchor('admin/addgallery','Add Gallery');?&gt;&lt;/li&gt;
              &lt;li&gt;&lt;?php echo anchor('admin/uploadimage','Upload Image');?&gt;
              &lt;/li&gt;
              &lt;li&gt;&lt;?php echo anchor('#','Delete Gallery');?&gt;&lt;/li&gt;
          &lt;/ul&gt;
          &lt;/li&gt;
          &lt;li&gt;&lt;?php echo anchor('#','Sales');?&gt;
          &lt;ul&gt;
              &lt;li&gt;&lt;?php echo anchor('admin/addsale','Add Sale');?&gt;&lt;/li&gt;
              &lt;li&gt;&lt;?php echo anchor('#','Edit Sale');?&gt;
              &lt;?php if(is_array($sales_pages)): ?&gt;
                  &lt;ul&gt;
                      &lt;?php foreach($sales_pages as $sale): ?&gt;
                      &lt;li&gt;&lt;a href="editsale/index/&lt;?=$sale-&gt;id?&gt;"&gt;&lt;?= $sale-&gt;name?&gt;&lt;/a&gt;&lt;/li&gt;
                      &lt;?php endforeach; ?&gt;
                  &lt;/ul&gt;
              &lt;?php endif; ?&gt;
              &lt;/li&gt;
  
              &lt;li&gt;&lt;?php echo anchor('#','Delete Sale');?&gt;&lt;/li&gt;
              &lt;li&gt;&lt;?php echo anchor('admin/home/logout','Log Out');?&gt;&lt;/li&gt;
          &lt;/ul&gt;
          &lt;/li&gt;
      &lt;?php else: ?&gt;
      &lt;ul&gt;
      &lt;li&gt;&lt;?php echo anchor('home','Home');?&gt;&lt;/li&gt;
      &lt;li&gt;&lt;?php echo anchor('about','About Us');?&gt;&lt;/li&gt;
      &lt;li&gt;&lt;?php echo anchor('gallery','Gallery');?&gt;&lt;/li&gt;
      &lt;li&gt;&lt;?php echo anchor('testimonials', 'Testimonials');?&gt;&lt;/li&gt;
      &lt;li&gt;&lt;?php echo anchor('sales','Sales');?&gt;&lt;/li&gt;
      &lt;li&gt;&lt;?php echo anchor('contact','Contact Us');?&gt;&lt;/li&gt;
      &lt;/ul&gt;
      &lt;?php endif; ?&gt;
      &lt;/ul&gt;
  &lt;/nav&gt;
   &lt;section id="content"&gt;
      &lt;h1&gt;&lt;?= $title ?&gt;&lt;/h1&gt;
  
      &lt;p&gt;&lt;?= $content ?&gt;&lt;/p&gt;
  
  &lt;/section&gt;
  
  &lt;footer&gt;&amp;copy; Houses LTD &lt;?php echo date('Y');?&gt;&lt;/footer&gt;
  &lt;/div&gt; &lt;!-- Wrapper Close --&gt;
  
  &lt;/body&gt;
  </code></pre>