Take the 2-minute tour ×

I am trying to create dynamic pages without creating new files and getting the data from the database...

so my table will be like:

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?

share|improve this question
Have a look at hooks. –  Rocket Hazmat Jun 4 '12 at 18:10
add comment

1 Answer

up vote 1 down vote accepted

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)

            // Does exist. Do things.
        } else

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

share|improve this answer
thanks Sebastian. That's another way to do this. –  fxuser Jun 4 '12 at 18:15
add comment

Your Answer


