CodeIgniter Forums
How to pass joined table value in view.php? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: How to pass joined table value in view.php? (/showthread.php?tid=71363)



How to pass joined table value in view.php? - HarrysR - 08-05-2018

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!


RE: How to pass joined table value in view.php? - puschie - 08-06-2018

pls provide sample code


RE: How to pass joined table value in view.php? - HarrysR - 08-06-2018

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".


RE: How to pass joined table value in view.php? - Pertti - 08-06-2018

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();
   } 



RE: How to pass joined table value in view.php? - HarrysR - 08-06-2018

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


RE: How to pass joined table value in view.php? - Pertti - 08-06-2018

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.


RE: How to pass joined table value in view.php? - HarrysR - 08-06-2018

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!!