简体   繁体   中英

How to show titles gotten from my database for my application blogposts

I am a newbie in codeigniter, I want to show dynamic titles gotten from my database for my application blogposts. i tried the below code but i kept getting "undefined variable error".

Below is the controller

function blogpost(){
$entry_slug = $this->uri->segment(2);

    $config['total_rows'] = $this->Client_model->get_post_count();
    $config['per_page'] = 5;
   
$data['queries'] = $this->Client_model->get_all_post($config['per_page']);

$result = $this->Client_model->get_post($entry_slug,$entry_name); 

$data['title'] = $result['entry_name'];

$data['post']= $this->Client_model->get_post($entry_slug,$entry_name);
$this->load->view('templates/headerss', $data);
$this->load->view('pages/blogpost');
$this->load->view('templates/footerss');
}

The model is

    function get_post($entry_slug,$entry_name){
$this->db->select();
$this->db->from('entry');
$this->db->where('entry_slug',$entry_slug);
$post = $this->db->get();
return $post->result_array(); }

my view is

 <title><?php if (!empty($title)) echo $title;?></title>

Please help me out, I have been stuck on this for days. I really need the title to change from the database table(column "entry_name") as each blog post is clicked on. thank you

If you decided to use return $post->result_array(); in your model you have to fetch the result first in your controller using foreach(). Like so:::

foreach($result as $row){
$data['title'] = $row['entry_name'];
}

OR ANOTHER SOLUTION:

Change your model : $post->result_array(); to $post->row_array()

It should work without using foreach inside your controller. because it only fetch 1 row of your query result array to $data['title'] = $result['entry_name']; .

Another solution:

$post->row(); then in your controller use like so $data['title'] = $result->entry_name;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM