Search code examples
mysqldatabasecodeigniternormalization

Normalization , Category Listing and Code igniter


I have three tables

articles enter image description here

categories

enter image description here

and relationship table articles_categories

enter image description here

that table is where I store my relationship like this using normalization features.Like this enter image description here

I was able to do select,insert,update, delete . Here are my previous questions Insert , Update , Delete -> Normalization & Code Igniter Saving articles and related categories according to normalization

What I am trying to achieve now is I am trying to create a listing of articles via category. Like this

Category Name
- article relating to that category
- article relating to that category
- article relating to that category
- article relating to that category

This is my controller

function category($id)
        {
            $data['title'] = "View by category";
            $data['articles_categories']=$this->articles_categories_model->get_by_id($id);
            $data['articles']=$this->articles_model->get();
            $data['category_id']=$id;

            $data['categories_list']=$this->categories_model->get();     
            $this->load->view('articles_pages/category',$data);    
        }

My view

<?php
    //$category_id;
    foreach($articles as $a)
    {
        foreach($articles_categories as $ac)
        {
            if($ac['categories_id']==$category_id && $a['id']==$ac['articles_id'])
            {
                echo $a['title'];
            }
        }
    }
?>

I can't get the desire result. Please help me. Thanks


Solution

  • As the category method takes an $id as a parameter, I am going to assume that you only want to display articles for a single category.

    You are currently returning data that you don't want, and then filtering it out in your view. This is inefficient (and could become a problem as the amount of articles increases), and it is not the correct approach to MVC programming. Instead of returning every article and then filtering through the data, we should concentrate on getting the correct data in the model and passing that to the view.

    Controller:

    function category($id) 
    {
       $data['title'] = "View by category";
       $data['articles'] = $this->articles_model->get_articles_by_category(id);
       $data['category_id']=$id;
    
       $data['categories_list']=$this->categories_model->get();     
       $this->load->view('articles_pages/category',$data);    
    }
    

    Model:

    function get_articles_by_category($id) 
    {
       $this->db->select('*');
       $this->db->from('articles');
       $this->db->join('articles_categories', 'articles.id = articles_categories.articles_id');
       $this->db->where('articles_categories.categories_id', $id);
       $query = $this->db->get();
    
       return $query;
    }
    

    In the above we join the articles table, with the articles_categories table, allowing us to get articles from a certain category. The above is just an example using the active-record class, but feel free to re-write it in whichever style your comfortable with.