Reputation: 8385
Update:
I have tried Sheldon's code below but I have struck a couple of issues:
When using the line $this->load->view($page->template, array('content' => $page->content), TRUE);
Unable to load the requested file .php but if I change it to $page->content
it works fine.
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?
Original Question:
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 $title
and the $content
data from the database.
I have set a view for each page and also a controller. I have tried:
$this->cms_pages->name;
This produces nothing. I know I need to get the URI segment but because I am using /$sales->name
I am unsure if I have to change it to $sales->id
for this to work. I would like to get it by the name. Best practices are also important.
Is there a way that I could do something like: $this->cms_pages->id->1->name
?
Model
function getCMSPages() {
$query = $this->db->get('pages');
if($query->num_rows() > 0) return $query->result();
}
Template
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
public function index()
{
$data['cms_pages'] = $this->navigation_model->getCMSPages();
$data['title'] = 'Ccho';
$data['content'] = $this->load->view('home', NULL, TRUE);
$this->load->view('template', $data);
}
}
Template
$stylesheetCSS = array('href' => 'includes/css/style.css', 'rel' => 'stylesheet', 'type' => 'text/css', 'media' => 'print');
echo doctype('html5');
?>
<head>
<title>Housemovers : <?php echo $title ?></title>
<?php echo link_tag($stylesheetCSS);?>
</head>
<body>
<div id ="wrapper">
<header>
<p>content</p>
</header>
<nav>
<ul>
<?php if($this->session->userdata('logged_in')): ?>
<li><?php echo anchor('#','Edit Pages');?>
<?php if(is_array($cms_pages)): ?>
<ul>
<?php foreach($cms_pages as $page): ?>
<li><a href="<?=$page->title?>"><?= $page->title?></a></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<li><?php echo anchor('#','Gallery');?>
<ul>
<li><?php echo anchor('admin/addgallery','Add Gallery');?></li>
<li><?php echo anchor('admin/uploadimage','Upload Image');?>
</li>
<li><?php echo anchor('#','Delete Gallery');?></li>
</ul>
</li>
<li><?php echo anchor('#','Sales');?>
<ul>
<li><?php echo anchor('admin/addsale','Add Sale');?></li>
<li><?php echo anchor('#','Edit Sale');?>
<?php if(is_array($sales_pages)): ?>
<ul>
<?php foreach($sales_pages as $sale): ?>
<li><a href="editsale/index/<?=$sale->id?>"><?= $sale->name?></a></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<li><?php echo anchor('#','Delete Sale');?></li>
<li><?php echo anchor('admin/home/logout','Log Out');?></li>
</ul>
</li>
<?php else: ?>
<ul>
<li><?php echo anchor('home','Home');?></li>
<li><?php echo anchor('about','About Us');?></li>
<li><?php echo anchor('gallery','Gallery');?></li>
<li><?php echo anchor('testimonials', 'Testimonials');?></li>
<li><?php echo anchor('sales','Sales');?></li>
<li><?php echo anchor('contact','Contact Us');?></li>
</ul>
<?php endif; ?>
</ul>
</nav>
<section id="content">
<h1><?= $title ?></h1>
<p><?= $content ?></p>
</section>
<footer>© Houses LTD <?php echo date('Y');?></footer>
</div> <!-- Wrapper Close -->
</body>
Upvotes: 1
Views: 10088
Reputation: 972
As @davzie said;
Model
function getCMSPage($page_name = '') {
if(!$page_name) $page_name= 'home'; // home be default.
$this->db->where('name', $page_name);
$query = $this->db->get('pages', 1);
if($query->num_rows() == 1) return $query->row();
}
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
public function index()
{
$page = $this->navigation_model->getCMSPage($this-uri->segment(1)); // pretend the first segment in your URI is the page you want.
$data['cms_pages'] = $this->navigation_model->getCMSPages();
$data['title'] = $page->name;
$data['content'] = $this->load->view($page->template, array('content' => $page->content), TRUE);
$this->load->view('template', $data);
}
}
Upvotes: 3
Reputation: 116
You might want to consider having a function that gets the details of a page by its name. This in-turn would run a function like getIdByName($page_name = false)
From there you would have the ID to pull from the database and get the correct page.
Upvotes: 2