在codeigniter php中动态显示基于页面的页面标题

How to display page titles dynamically based on the pages displayed.

Hi i am having a site developed in codeigniter php but the problem is need to display page titles dynamically based on the pages.These pages titles should be fetched from database.Can anyone have any idea how to do this.

Controller:

public function index()
{

    $config = array();
    $config["base_url"] = base_url('testimonial/index');
    $config['total_rows'] =   $this->testimonial_model->record_count();//here we will count all the data from the table
    $config['per_page'] = 6;//number of data to be shown on single page
    $config['first_link'] = 'First';
    $config['last_link'] = 'Last';
    $this->pagination->initialize($config);
    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
    $data["records2"] = $this->testimonial_model->get_all_testimonials($config["per_page"], $page);
    $data['records7'] = $this->index_model->get_all_banners();
    $data["links"] = $this->pagination->create_links();//create the link for pagination
    $data['mainpage'] = "testimonial";
    $this->load->view('templates/template',$data);
}

Model:

function get_all_testimonials($limit, $start)
{
    $this->db->limit($limit, $start);
    $this->db->select('T.*');
    $this->db->from('testimonials AS T');
    $this->db->where(array('T.status'=>1));
    $q = $this->db->get();
    if($q->num_rows()>0)
    {
        return $q->result();
    }
    else 
    {
        return false;
    }
}

View:

<div class="container"> 
<div class="row testimonialpage"> 
<div class="col-md-12 testimonialpage">         
    <div class="col-md-9 testimonials" >    
    <div class="testimonialpagetext">       
    </div>  
    <?php if(isset($records2) && is_array($records2)):?>    
    <?php foreach ($records2 as $r):?>          
    <div class="testimonial1">          
    <div class="testimonialtext1">      
    <?php echo $r->description;?>   
    <ul class="founders">
        <li class="founder"><?php echo $r->client_name;?></li>
        <li class="founder"><?php echo $r->founder;?></li>          
    </ul>
    </div>      
    </div>      
    <?php endforeach ;endif;?>  
    <div class="pagination"><?php echo $links; ?></div> 
    </div>  
    </div>      
    </div> 

For Ex:: Your url is www.example.com/controller/register when you get uri segment(2) means you will get (register). based on this you can identify that you are on the register page. now you can find the title for register page from your database.

Every time i build an application with Codeigniter, i use a Class MY_Controller that extends CI_Controller, inside application/core like bellow:

class MY_Controller extends CI_Controller {
    protected $view_data;

    function __construct() {
        //......
    }
}

Any controller inside application/controllers will extend the MY_Controller instead of CI_Controller.

Now in the constructor of MY_Controller you can do something like:

$page = $this->uri->segment(2); (2 or 3 or 4 etc, depends on your structure)

Load your model, execute your query and add the result in $this->view_data like:

$pageTitle = $this->your_model->getPageTitle($page);
$this->view_data['page_title'] = $pageTitle['page_title'];

Example of getPageTitle in your model:

public function getPageTitle($page) {
    $qry = $this->db->select('page_title')
                ->from('pages')
                ->where('page', $page)
                ->get();
        if ($qry->num_rows() > 0)
            return $qry->row_array();
        return false;
}

Watch out the sample names i gave, you should change those!

To load your view, pass the variable $this->view_data as parameter and in your view do something like

<title><?= $page_title ?></title>