Reputation: 5860
I am trying to create dynamic pages without creating new files and getting the data from the database...
so my table will be like:
Pages
------
id | page_name | text
1 | about | about page goes here
2 | contact | contact page goes here
now my question is how can i manage to make $this->uri->segement(1)
to automatically check if the given page name exist in database?
do i have to create a new controller that will handle all this or?
Upvotes: 1
Views: 1200
Reputation: 1983
If you have several pages that you want to check, I would recommend having a Pages controller that managers those pages. Something like this
class Pages extends CI_Controller {
public function view($page_name)
{
$this->load->Pages_model();
if($this->Pages_model->does_exist($page_name))
{
// Does exist. Do things.
} else
{
show_404();
}
}
}
In your routes.php
, you route your about
and contact
pages (and whatever others you may have) to the pages controller.
$route['about'] = "pages/view/about";
$route['contact'] = "pages/view/contact";
Your Pages_model
will need a simple function that checks if the page name exists in the database.
function does_exist($page_name) {
$this->db->where('name', $page_name); // assuming you have a table with a `name` field
$query = $this->db->get('pages'); // select from the `pages` table
return $query->num_rows() > 0; // returns bool
}
Upvotes: 1