codeigniter check if uri segment exist in database -


i trying create dynamic pages without creating new files , getting data database...

so table like:

pages ------ id | page_name | text 1  |     | page goes here 2  | contact   | contact page goes here 

now question how can manage make $this->uri->segement(1) automatically check if given page name exist in database?

do have create new controller handle or?

if have several pages want check, recommend having pages controller managers pages. this

class pages extends ci_controller {     public function view($page_name)     {         $this->load->pages_model();          if($this->pages_model->does_exist($page_name))         {             // exist. things.         } else         {             show_404();         }     } } 

in routes.php, route about , contact pages (and whatever others may have) pages controller.

$route['about'] = "pages/view/about"; $route['contact'] = "pages/view/contact"; 

your pages_model need simple function checks if page name exists in database.

function does_exist($page_name) {      $this->db->where('name', $page_name); // assuming have table `name` field     $query = $this->db->get('pages'); // select `pages` table     return $query->num_rows() > 0; // returns bool  } 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -