Welcome Guest, Not a member yet? Register   Sign In
How to pass joined table value in view.php?
#1

Hello,
I've created a blog and i want to pass a value from a joined table in view.php.
e.g.: I have "posts" and "categories" and it properly displays in index.php but when i click an individual article and trying to pass the same value there, i get "undefined index: category_name".

Any help with that?!

Thank you in advance!

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply
#2

pls provide sample code
Reply
#3

Yes sorry my mistake!

My view Controller

Code:
   public function view($slug = NULL){

     $data['post'] = $this->post_model->get_posts($slug);

     if(empty($data['post'])){
       show_404();
     }

     $data['title'] = $data['post']['post_title'];

     $this->load->view('templates/header');
     $this->load->view('posts/view', $data);
     $this->load->view('templates/footer');
   }


My post Model
Code:
   public function get_posts($slug = FALSE){
     if($slug === FALSE){
       $this->db->order_by('post_id', 'DESC');
       $this->db->join('post_categories', 'post_categories.post_category_id = posts.post_category_id');
       $query = $this->db->get('posts');
       return $query->result_array();
     }

     $query = $this->db->get_where('posts', array('post_slug' => $slug));
     return $query->row_array();
   }

After that, i enter in view.php for each individual post and when i try to echo "category_name" which is stored in different table and what i get is "undefined index: category_name".

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply
#4

(This post was last modified: 08-06-2018, 06:08 AM by Pertti.)

You'll need to factor in $this->db->join('post_categories', 'post_categories.post_category_id = posts.post_category_id'); for both if slug is false and when slug is provided.

Something like should work:

PHP Code:
public function get_posts($slug FALSE){
     
$this->db->join('post_categories''post_categories.post_category_id = posts.post_category_id');
     
     if(
$slug === FALSE){
       
$this->db->order_by('post_id''DESC');
       
$query $this->db->get('posts');
       return 
$query->result_array();
     }

     
$query $this->db->get_where('posts', array('post_slug' => $slug));
     return 
$query->row_array();
   } 
Reply
#5

Thank you very much! It worked like a charm!!

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply
#6

Glad it worked Smile

That's one of the strong sides of query builder IMHO, you can easily stack it up in different condition-blocks, but still apply common relation logic.
Reply
#7

I wanted to pass many different info (like user, category, city etc.) for a single post (and not only blog posts) and i couldn't figure it out for like 4-5 days.

I'm kinda new in codeigniter and i'm still learning it while i build a project! I have lots of things to learn and lots of topics to open! Tongue

Thanks again!!

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply




Theme © iAndrew 2016 - Forum software by © MyBB