Welcome Guest, Not a member yet? Register   Sign In
How to GET number of posts for each category with type category_post in the view
#1

hello team, I'm working on a project using Codeigniter and I want to display the following data by showing the number of data in real time. I have a TABLE named POST which contains id, title, content, category_id and a TABLE named CATEGORY which contains id, category_name, category_type. So i would like to display the total of POSTS recorded for each CATEGORY selected in the request, because the CATEGORY table is used for other tables, so I want to have just categories which relates to POSTS, the query is fine but i dont know how to send the data to the view.
I have difficulties about how to display data in VIEW here an image on how I want to have the data, any help will be appreciated Thank you for your assistance, my respects
Code:
here is my Model

    class My_model extends CI_Model {
   
        function get_post_by_category() {
            $q = $this->db->select(' POST.CATEGORY_ID, CATEGORY.CATEGORY_NAME, COUNT(POST.CATEGORY_ID) as total_posts,  COUNT(POST.CATEGORY_ID)/COUNT(*) * 100 as percentage')
                          ->from('POST')
                          ->join('CATEGORY', 'POST.CATEGORY_ID= CATEGORY.ID', 'left')
                          ->where('CATEGORY.CATEGORY_TYPE', 'TYPE_POST')
                          ->group_by('POST.CATEGORY_ID, CATEGORY.CATEGORY_NAME')
                          ->order_by('POST.CATEGORY_ID', 'ASC')
                          ->get();
            return $q->result();
        }
    }
here is my controller

public function __construct() {
        parent::__construct();
        $this->load->model('My_model');
    }

    function index() {
        $this->load->model('My_model');
        //config
        $config['base_url'] = base_url('/?c=ui_page');
        //load the method of model 
        $data['countdata']=$this->My_model->get_post_by_category();
        //return the data in view 
        $this->load->view('data/my_view', $data);
    }
here is my view

<table border="1"> 
      <tbody> 
        <tr> 
            <td> CATEGORY NAME</td> 
            <td> Total POSTS</td>
            <td> %</td> 
        </tr> 
        <?php 
        foreach ($countdata as $row1) 
        { 
            ?><tr> 
            <td><?php echo $row1->CATEGORY_NAME;?></td> 
            <td><?php echo $row1->ID;?></td> 
            <td>%</td> 
            </tr> 
        <?php } 
        ?> 
      </tbody> 
  </table>
Reply
#2

Change

Code:
<tr>
    <td> CATEGORY NAME</td>
    <td> Total POSTS</td>
    <td> %</td>
</tr>

To:

Code:
<tr>
    <td> CATEGORY NAME</td>
    <td> Total POSTS</td>
    <td><?php echo count($countdata);?></td>
</tr>
Reply




Theme © iAndrew 2016 - Forum software by © MyBB