Managing your page tags in Codeigniter

July 16th, 2020

How to give yourself complete granular control over your page tags within a Codeigniter project

In this example we will be working to setup and control the following page tags, however, you will see that the same technique can be easily adapted to set any tags within your pages.

Firstly we setup the database table with our fields that will be used:


CREATE TABLE `pages` (
`pages_id` int(11) NOT NULL AUTO_INCREMENT,
`pages_name` varchar(45) DEFAULT NULL,
`pages_controller` varchar(45) DEFAULT NULL,
`pages_method` varchar(45) DEFAULT NULL,
`pages_slug` varchar(100) DEFAULT NULL,
`pages_title` varchar(60) DEFAULT NULL,
`pages_meta_description` varchar(250) DEFAULT NULL,
`pages_meta_keywords` varchar(250) DEFAULT NULL,
`pages_content` blob,
PRIMARY KEY (`pages_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;



Now we need the following function in whichever model will be included to find our page tags information:

For this example I am going to create a standalone model 'Page_model.php'


class Page_model extends CI_Model {
public function __construct(){
// Call the CI_Model constructor
parent::__construct();
}

public function get_meta($page){
$this->db->from('pages');
$this->db->where('pages_name', $page);
$this->db->limit(1);
$query = $this->db->get();
if($query->num_rows() > 0){
return $query->row_array();
} else {
//It is a good idea to provide some defaults just in case your database does not have a result for this search/page
$meta = array('pages_title' => ucfirst($page).' | Redwarp Internet', 'pages_meta_description' => '', 'pages_meta_keywords' => '');
return $meta;
}
}
}

Finally, in our controller, we need to include our model from above:

		$this->load->model('Page_model');

Calling the function from within our controller is then a simple process as follows:

		$meta = $this->Page_model->get_meta($page_name);
$page_content['meta_title'] = $meta['pages_title'];
$page_content['meta_description'] = $meta['pages_meta_description'];
$page_content['meta_keywords'] = $meta['pages_meta_keywords'];
//load our view as normal
$this->load->view('templates/template_view', $page_content);

Now in our view, something like this in the <head> section to build the required page tags:

<head> 
  <title><?= $meta_title ?></title>
  <meta name="description" content="<?= $meta_description ?>">
  <meta name="keywords" content="<?= $meta_keywords ?>">
</head>

Contact

You can contact us by completing the form below or email us directly if you prefer

Email Us

info@redwarp.com

Loading