skip to Main Content

Hi, I am stuck on a problem where i have to echo dynamic meta
description and meta keywords for a specific page. I have included the
header file into the constructor and it contains a code that has
language change option through session.

I have a table named contents which contains content of the pages and
also 2 columns (metakeywords,metadescription). I want them to
dynamically display on each page where i get the id of the page.

Below is the code so far : please help

This is controller :

public function __construct(){
$this->load->model('home_m');
$data['query'] = $this->home_m->seo($id);
$this->load->view('site/include/head', $data);
$header = $this->header();
$this->load->view('site/include/header', $header);
}

public function header(){        
        $site_setting = $this->home_m->getRow('general',array('gen_id' => 1));
        $data['site_setting'] = $site_setting;
        $header_links="select * from ".PRE_FIX."header_links";
        $data['header_links'] = $this->home_m->getCustomRecords($header_links);
$this->set_language();
if ( $site_setting->language == 1){
            $language = "english";
        } else {
            $language = "french";
        }

        if ( $this->session->userdata("setLanguage") != ""   ) {
            $language = $this->session->userdata("setLanguage");
        }
        $data['language'] = $language;
         return $data;
    }

Now for eg: i want to display the header metakeywords and meta description on the following page which is webhosting.

public function webhosting(){
$this->set_language();
$language =  $this->session->userdata("setLanguage");
$get_plans = "Select * from gdn_planstesting where page='webhosting'";
$data['get_plans'] = $this->home_m->getCustomRecords($get_plans);
$this->load->view('site/webhosting', $data);
$this->load->view('site/include/footer',$data);
}

This is model :

public function getCustomRecords($customQuery)
    {
        $recordSet = $this->db->query($customQuery);
        return $recordSet->result();
    }

function seo($id){
$query = $this->db->query("SELECT * FROM gdn_planstesting WHERE planstestingid='$id'");
return $query->result_array();
    }

This is view :

foreach($get_plans as $pl) {
    $pid = $pl->planstestingid;
    $pname = $pl->planname;
    $planStatus=$pl->plan_status;
}

foreach($query as $value){
echo $query->id;
}
**This is the header file :**

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="description" content="" />
    <meta name="keywords" content="">
    <link rel="stylesheet" href="<?php echo base_url();?>assets/front/css/style.css" type="text/css">
    <link rel="stylesheet" href="<?php echo base_url();?>assets/front/css/bootstrap.css" type="text/css">
    <link rel="stylesheet" href="<?php echo base_url();?>assets/front/css/font-awesome.css" type="text/css">
    <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,600,300,700' rel='stylesheet' type='text/css'>
    <link href='https://fonts.googleapis.com/css?family=Oswald:400,300,700' rel='stylesheet' type='text/css'>
    <title>Your title</title>
    </head>
    <body>
        <header>
            <div class="container-fluid headertop">
            <!-- <div class="container-fluid"> -->
                <div class="container">
                    <div class="row">
    <?php
        if($language == "english"){

            $lang1 = "class='active'";
            $lang2 = "";
        } else {

            $lang1 = "";
            $lang2 = "class='active'";
        }?>
            </div>
                </div>
            </div>
        </header>

2

Answers


  1. Chosen as BEST ANSWER

    I finally Solved the issue by doing the following code: Although its not dynamic but fulfills my requirements. Thankyou @dnapierata for your help. i couldnt have done it without your help trust me.

    public function header(){    
    
        $ur = $this->uri->segment(1);
       // echo $ur;
    
            $site_setting = $this->home_m->getRow('general',array('gen_id' => 1));
            $data['site_setting'] = $site_setting;
            $header_links="select * from ".PRE_FIX."header_links";
            $data['header_links'] = $this->home_m->getCustomRecords($header_links);
    
    
    
            $this->set_language();
    
            if ( $site_setting->language == 1){
                $language = "english";
            } else {
                $language = "french";
            }
    
            if ( $this->session->userdata("setLanguage") != ""   ) {
                $language = $this->session->userdata("setLanguage");
            }
    
            $data['language'] = $language;
    
    
    
    
           if($ur != 'resellerhosting' || $ur != 'businesshosting' || $ur != 'webhosting' || $ur != 'domain' || $ur != 'aboutus' || $ur != 'contactus'){
    
            $this->set_language();
    
    
            $language =  $this->session->userdata("setLanguage");
    
    //echo "select * from ".PRE_FIX."content where page_name='$ur' and content_language = '".$language."'";
    
            $ss="select * from ".PRE_FIX."content where page_name='$ur' and content_language = '".$language."'";
            $data['ss'] = $this->home_m->getCustomRecords($ss);
    
    }
    
    if($ur == 'resellerhosting' || $ur == 'businesshosting' || $ur == 'webhosting' || $ur == 'domain' || $ur == 'aboutus' || $ur == 'contactus' || $ur == ''){
    
    
    
    //echo "select * from ".PRE_FIX."planstesting where page='$ur'";
    
            $sd="select * from ".PRE_FIX."planstesting where page='$ur'";
            $data['sk'] = $this->home_m->getCustomRecords($sd);
    
    }
    

  2. In your header method in your controller you must first make a call to your model which should query your database for the meta information that you need.

    Controller:

    public function __construct(){
        $this->load->model('home_m');
        $header = $this->header();
        $this->load->view('site/include/header', $header);
    }
    
    public function header(){        
        $site_setting = $this->home_m->getRow('general',array('gen_id' => 1));
        $data['site_setting'] = $site_setting;
        $header_links="select * from ".PRE_FIX."header_links";
        $data['header_links'] = $this->home_m->getCustomRecords($header_links);
        $this->set_language();
        if ( $site_setting->language == 1){
            $language = "english";
        } else {
            $language = "french";
        }
    
        if ( $this->session->userdata("setLanguage") != "" ) {
            $language = $this->session->userdata("setLanguage");
        }
        $data['language'] = $language;
    
        //Get the meta information from the model
        $data['meta'] = $this->home_m->seo($this->uri->segment(1), $this->input->get($id));
    
        return $data;
    }
    

    $this->router->method simply returns the method that was called for this controller, we can use this to determine which table to select from.

    Model:

    public function getCustomRecords($customQuery){
        $recordSet = $this->db->query($customQuery);
        return $recordSet->result();
    }
    
    function seo($table, $id){
        $query = $this->db->query("SELECT * FROM $table WHERE $table"."_id='$id'");
        return $query->row();
    }
    

    Once you have the information, then you can output it in your sites/include/header view.

    Header View:

    <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <meta name="description" content="<?php echo $meta->metadescription; ?>" />
        <meta name="keywords" content="<?php echo $meta->metakeywords; ?>">
        <link rel="stylesheet" href="<?php echo base_url();?>assets/front/css/style.css" type="text/css">
        <link rel="stylesheet" href="<?php echo base_url();?>assets/front/css/bootstrap.css" type="text/css">
        <link rel="stylesheet" href="<?php echo base_url();?>assets/front/css/font-awesome.css" type="text/css">
        <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,600,300,700' rel='stylesheet' type='text/css'>
        <link href='https://fonts.googleapis.com/css?family=Oswald:400,300,700' rel='stylesheet' type='text/css'>
        <title>Your title</title>
        </head>
        <body>
            <header>
                <div class="container-fluid headertop">
                <!-- <div class="container-fluid"> -->
                    <div class="container">
                        <div class="row">
        <?php
            if($language == "english"){
    
                $lang1 = "class='active'";
                $lang2 = "";
            } else {
    
                $lang1 = "";
                $lang2 = "class='active'";
            }?>
                </div>
                    </div>
                </div>
            </header>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search