简体   繁体   中英

How to Count rows If Current Column `Id` is Matching from Another Table in Codeignitor PHP?

I'm developing a School erp in Codeignitor. I'm facing an issue while counting the no of rows. Here Let me explain.

This is my Class Table.

在此处输入图像描述

Here, this is the student Table.
在此处输入图像描述


and this is my view在此处输入图像描述

So, I want to count student from table student and print the number in box below the class name. as you can see that the id fro table class is matching from the clumne class_id in Table Student.

Here is my Model Code:-

 //get class list in box 
     function getClassList() {
        $query = $this->db->select('*')->get('classes');
        return $query->result_array();
    }

My Controller:-

function classlist() {

    if (!$this->rbac->hasPrivilege('student', 'can_view')) {
        access_denied();
    }
    $this->session->set_userdata('top_menu', 'Student Information');
    $this->session->set_userdata('sub_menu', 'student/search');
    $data['title'] = 'Student Search';
    $clist = $this->student_model->getClassList();
    $data['clist'] = $clist;
        $this->load->view('layout/header', $data);
        $this->load->view('student/studentSearch', $data);
        $this->load->view('layout/footer', $data);
    }
}

and here My view page:-

            <?php foreach ($clist as $key  ) {
                ?>


                <div class="info-box">
                    <a href="<?php echo base_url();?>student/stdlists/<?php echo $key['id']; ?>">
                        <span class="info-box-icon bg-green"><i class="fa fa-child"></i></span>
                        <div class="info-box-content">
                            <span class="info-box-text"><?php echo $key['class']; ?></span>

                            <span class="info-box-number">**I want to Print The Total Count here**</span>
                        </div>
                    </a>
                </div>     

        <?php } ?>
        </div>

you will need left join and group by in function getClassList.

Final query shoud be something like this:

select count(t.id) as students_in_class, s.*
from class s
left join student t on s.id = t.class_id
group by t.class_id

but you using ORM and I not familiar with it....

Hope this will work for you..

$this->db->where('id','class_id');
$this->db->get('student');
$number_of_rows=$this->db->num_rows();
print_r($number_of_rows);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM