I am writing an application in CodeIgniter where I specify the
meta-tag on every page in every controller which I have managed to send to my header te
A simple example:
Controller
$query = $this->Listing_model->get_card($card);
$query = $query->row();
$header["page_title"] = $query->title;
View
<title><?php echo (!isset($page_title) ? '' : $page_title) ?></title>
Controller
$card_data= $this->Listing_model->get_card($card); //Your model returns an array of objects
$header["page_title"] = $card_data[0]->title; //grab value of 'title' property of first object returned from model.
$this->load->view('includes/header',$header);
View
<title><?php echo (!isset($page_title) ? '' : $page_title) ?></title>
You may need to create some routes for your show function. Codeigniter URI Routing
$route['your_controller_name/show/(:any)'] = 'your_controller_name/show/$1';
I am not sure if you have set up a htaccess for your main directory so you could remove the index.php
from your url.
Try this code below
Model:
<?php
class Listing_model extends CI_Model {
function get_card_title($card) {
$this->db->where('slug', $card);
$query = $this->db->get($this->db->dbprefix . 'creditcards');
if ($query->num_rows() > 0) {
$row = $quer->row();
return $row->title;
} else {
return false;
}
}
}
Controller: Your_controller_name.php
<?php
class Your_controller_name extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('listing_model');
}
public function show($card) {
$data['title'] = $this->listing_model->get_card_title($card);
$this->load->view('includes/header', $data);
$this->load->view('listings/listing_card', $data);
$this->load->view('includes/footer');
}
}
View:
<head>
<title><?php echo $title;?></title>
</head>
Try this:
function get_card($card = FALSE)
{
$data = $this->db->get_where('creditcards', array('slug' => $card), 0,1)->result();
$data->title = $data[0]->title;
return $data;
}
Controller
$query = $this->Listing_model->get_card($card);
var_dump($query);
//Your $query may be some data got from db;
$card_name = "";
if(!empty($query)){
$card_name = $query[0]->name; //You must verify the name attribute and it should in the $query result;
}
$header["page_title"] = $card_name;
View
<title><?php echo (!isset($page_title) ? '' : $page_title) ?></title>
You can create a Base Controller and Extends all you other controller to that base controller.
Like this
<?php
class MY_Controller extends CI_Controller {
public $data = array();
function __construct() {
parent::__construct();
$this->data['errors'] = array();
$this->data['site_name'] = config_item('site_name');
}
}
Then In Your Controller
class Test extends MY_Controller
{
function __construct() {
parent::__construct();
$this->data['meta_title'] = 'Your Title';
}
}
And in you views access the page title like this:
echo("<title>.$site_name.</title>");