0

Fetching page title from database getting error as

A PHP Error was encountered Severity: Notice Message: Trying to get property of non-object Filename: controllers/digital_marketing.php Line Number: 20

A PHP Error was encountered Severity: Notice Message: Trying to get property of non-object Filename: controllers/digital_marketing.php Line Number: 21

I am having two tables like 1.digital_marketing 2.pagetitle

In the first table i am inserting the data related to digital marketing along with digitalmarketing_name(The table will be in the following format)

digital_id  description   digitalmarketing_name
1           dfhbsdjbfd     digital_marketing

Second Table:(pagetitle)

pagetitle_id  page_title            title
1             digital_marketing     Digital Marketing

In this i am comparing page_title if both the page_titles match then i need to display title name but while comparing that getting an error which i have posted above.

If i am using underscore(_) in the page title it is getting that error if not it is working fine.

Controller:

class Digital_marketing extends CI_Controller {
function __construct()
{ 
    parent::__construct();
    $this->load->model('index_model');
    $this->load->model('digitalmarketing_model');

}
public function index()
{

    $data['records2']=$this->digitalmarketing_model->get_digitalmarketing();
    $pageReult = $this->digitalmarketing_model->getpagetitle($this->uri->segment(1));
    $data['page_title']=$pageReult->title;
    $data['meta_tags']=$pageReult->meta_tags;

    $data['mainpage'] = "digital-marketing";
    $this->load->view('templates/template',$data);
}

Model:

function getpagetitle($id)
{

    $this->db->select('P.*,D.digitalmarketing_name');       
    $this->db->from('pagetitle AS P');      
    $this->db->join('digital_marketing AS D','D.digitalmarketing_name=P.page_title','INNER');
    $this->db->where(array('P.page_title'=>$id));       
    $q=$this->db->get();        
    //var_dump($this->db->last_query());
    //print_r($q->num_rows());
    if($q->num_rows()>0)
      {
    $output = $q->result();

   return $output[0];
        }
    else
    {
    return false;
    }
}

The pagetitle which i have inserted in digital_marketing table it is my controller name.

4
  • Have u loaded model ? Commented Sep 21, 2017 at 7:07
  • @Gaurav i have loaded model Commented Sep 21, 2017 at 7:12
  • Please post your full controller code. Commented Sep 21, 2017 at 7:13
  • @NikuNjRathod Uploaded full controller code Commented Sep 21, 2017 at 7:20

1 Answer 1

1

You can change your modal function

Controller.php

    public function index()
        {

            $data['records2']=$this->digitalmarketing_model->get_digitalmarketing();
#echo $this->uri->segment(1); exit;
            $pageReult = $this->digitalmarketing_model->getpagetitle($this->uri->segment(1));
            $data['page_title']=$pageReult->title;
            $data['meta_tags']=$pageReult->meta_tags;

            $data['mainpage'] = "digital-marketing";
            $this->load->view('templates/template',$data);
        }

Modal : digitalmarketing_model.php

function getpagetitle($id) {
    $this->db->select('p.*,d.digitalmarketing_name');
    $this->db->from('digital_marketing AS d');
    $this->db->join('pagetitle as p', 'p.page_title = d.digitalmarketing_name', 'left');
    $this->db->where('p.page_title',$id);
    $query = $this->db->get();
    if ($query->num_rows() > 0) {
        $row = $query->row();
        return $row;
    } else {
        return false;
    }
}

I hope this will helps you.

Sign up to request clarification or add additional context in comments.

6 Comments

please post your segments $last = $this->uri->total_segments(); $record_num = $this->uri->segment($last);
Not getting you were i need to post this
Please add above code in your controller inside action.
I have added next to these line $data['meta_tags']=$pageReult->meta_tags; getting same error
Please print this in your controller action $this->uri->segment(1)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.