Dymanic navigation - URL question - El Forum - 09-25-2013
[eluser]ZioN[/eluser]
Hi,
I have an administration area in which I can dynamicly add or delete 'pages'. But when I want to load the pages I use their ID's. But those look horrible in the url not to mention SEO.
Controller(Site):
Code: public function page(){
$this->load->model('settings_model');
$this->load->model('pages_model');
$data['pages'] = $this->pages_model->get_page();
$data['settings'] = $this->settings_model->get_settings();
$this->load->view('page_view', $data);
if ($data = $this->pages_model->get_page()){
$this->load->model('settings_model');
$this->load->model('pages_model');
$data['pages'] = $this->pages_model->get_page();
$data['settings'] = $this->settings_model->get_settings();
}
else{
redirect('site');
}
}
Model(pages_model):
Code: public function get_page(){
$id = $this->input->get('id');
$query = $this->db->get_where('pages', array('id' => $id));
if ($query->num_rows() > 0){
$data = $query->result();
return $data;
}
else{
return false;
}
}
index_view:
Code: <?php
foreach($settings as $setting){
echo "<title>" . $setting->site_title . " • Settings</title>";
}
foreach($pages as $row){
echo "<ul>";
echo "<li><a id=". $row->id .">". $row->page_title ." ". $row->id ." </a></li>";
echo "</ul>";
}
?>
This is where I create the navigation (for testing purposes it's just an UL and LI)
page_view:
Code: <?php
foreach($settings as $setting){
echo "<title>" . $setting->site_title . " • Settings</title>";
}
foreach($pages as $row){
echo $row->page_title;
echo $row->content;
echo $row->author;
echo $row->datetime;
}
?>
When I click an item in the navigation it directs me to site/page?id=1 for example. This works fine, but I want to translate the id=1 to the actual "page_title". This would result in site/page/home for example.
PS: I only included relevant code.
Dymanic navigation - URL question - El Forum - 09-25-2013
[eluser]Otemu[/eluser]
Hi,
Quick example using your code above that should give you an idea that you can improve on:
Controller
Code: //get segment from url
$getPage = $this->uri->segment(2);
//pass segment to model
$data['pages'] = $this->pages_model->get_page($getPage);
Model
Code: public function get_page($getPage){
//query database for matching page title
$query = $this->db->get_where('pages', array('page_title' => getPage));
index_view:
Code: //display page title as url
foreach($pages as $row){
echo "<ul>";
echo "<li><a href="/page/">page_title.">/page/".$row->page_title." </a></li>";
echo "</ul>";
}
Dymanic navigation - URL question - El Forum - 09-25-2013
[eluser]ZioN[/eluser]
Thanks alot! It works. The only thing I have to do now is make the pages work. It shows a "not found" now.
Edit: It works perfectly.
Dymanic navigation - URL question - El Forum - 09-26-2013
[eluser]Otemu[/eluser]
Hi,
I offer some suggestions also to improve your controller:
Code: public function page(){
//loaded the model, no need to load them again further down the page in your if statement
$this->load->model('settings_model');
$this->load->model('pages_model');
//now you have the data stored in your variables no need to call this again
$data['pages'] = $this->pages_model->get_page();
$data['settings'] = $this->settings_model->get_settings();
//move your if statement, to something like if get_page exists load view else redirect
$this->load->view('page_view', $data);
//your making another call to the database, although you already have the data stored in
//$data['pages'], no need to make another call to database just use
//if ($data['pages'] ){
if ($data = $this->pages_model->get_page()){
//already loaded no need to load again
$this->load->model('settings_model');
$this->load->model('pages_model');
//remove this not needed
$data['pages'] = $this->pages_model->get_page();
$data['settings'] = $this->settings_model->get_settings();
}
else{
//codeigniter has a 404 function http://ellislab.com/codeigniter/user-guide/general/errors.html
//which you can call like this show_404();
//which is normally the standard way when a page is not found,
//you can custom the look of the page here
//application/errors/error_404.php
//of course if your site requires a redirect then use it
//some people else like to redirect on the model, as an alternative instead of
//returning false in your model you could just redirect straight away
//and then you won't even need to worry about if statement in your controller,
//but that depends on your own preference
redirect('site');
}
}
Dymanic navigation - URL question - El Forum - 09-26-2013
[eluser]ZioN[/eluser]
Thank you for your suggestions. I improved the code to:
Code: public function page(){
//Load models
$this->load->model('settings_model');
$this->load->model('pages_model');
// Grab the 3rd url segment - the segment after /page/
$getPage = $this->uri->segment(3);
$data['settings'] = $this->settings_model->get_settings();
$data['pages'] = $this->pages_model->get_page($getPage);
$this->load->view('page_view', $data);
// If page cannot be found, redirect to frontpage.
if (! $data['pages']){
redirect('site');
}
}
|