1

I want to display in View specific field value from Database.

For example my table:

                            id  |  rec_scope  |  rec_category
                            --------------------------
                            1   |  Product      |  New
                            2   |  Process      |  Enhance
                            3   |  Process      |  New
                            4   |  System       |  Enhance
                            5   |  Product      |  New
                            6   |  Process      |  New
                            7   |  Product      |  Enhance
                            8   |  System       |  New
                            9   |  Product      |  New
                            10  |  Prcduct      |  New

I got inspiration from:

COUNT / GROUP BY with active record? and Count and display how many of specific column result

Controller:

                            public function index()
                            {

                                $data['records_count']= $this->dashboard_m->records_count();
                                $data['records_scope']= $this->dashboard_m->records_scope();


                                $this->template->load('template', 'dashboard', $data);


                            }

Model:

                            public function records_scope() {
                                 $this->db->select('rec_scope, COUNT(rec_scope) as total');
                                 $this->db->group_by('rec_scope');
                                 $this->db->order_by('total', 'desc');
                                 $query = $this->db->get('records');


                                foreach ($query->result() as $row) 
                                    $data[] = array(
                                        'rec_scope'  => $row->rec_scope,
                                        'total'  => $row->total
                                    );



                                return $data;

                            }

How can I display in html view only the total count of specific field with specific value? Thank you.

Example:

Total count of Product is 5.

Total count Category of Product as New is 4.

Total count Category of Product as Enhance is 1.

6
  • what is your desired output? And your table example has a typo in Line 10: Prcduct should be Product, right? Commented May 11, 2019 at 16:55
  • @Vickel Yes..its just a typo..but it doesn't matter actually. Btw, I just need to display the value of total count of specific table field i.e Product. So the output will also display the total count of category "New" & "Enhance" Product..Tq Commented May 11, 2019 at 17:06
  • check #4 here: mysqltutorial.org/tryit/query/mysql-count/#4 Commented May 11, 2019 at 17:14
  • an example array output would be nice. i'm assuming you don't just want product. but e.g. array('product' => array('new => '4', 'enhance' => '1'), ('process' => .... etc ) ? Commented May 11, 2019 at 19:53
  • Thank you @Vickel. You give me some insight to progress. I will research more and try again Commented May 11, 2019 at 20:21

1 Answer 1

1

This would probably yield the results you are after:

    $this->db->select('*, COUNT(rec_scope) as count');
    $this->db->from('test');
    $this->db->group_by('rec_category, rec_scope');
    $this->db->order_by('rec_scope');
    $res = $this->db->get()->result();
    $data = array();
    foreach ($res as $item) {
        // initialize total
        if (!isset($data[$item->rec_scope]['Total'])) {
            $data[$item->rec_scope]['Total'] = 0;
        }
        $data[$item->rec_scope]['Total'] += $item->count;
        $data[$item->rec_scope][$item->rec_category] = $item->count;
    }
    echo '<pre>';
    print_r($data);

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.