简体   繁体   English

如果当前列“Id”与 Codeignitor PHP 中的另一个表匹配,如何计算行数?

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

I'm developing a School erp in Codeignitor.我正在 Codeignitor 中开发学校 erp。 I'm facing an issue while counting the no of rows.我在计算行数时遇到了问题。 Here Let me explain.这里让我解释一下。

This is my Class Table.这是我的 Class 表。

在此处输入图像描述

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.所以,我想从表学生中计算学生,并在 class 名称下方的框中打印数字。 as you can see that the id fro table class is matching from the clumne class_id in Table Student.如您所见,表 class 的id与表 Student 中的 clumne class_id匹配。

Here is my Model Code:-这是我的 Model 代码:-

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

My Controller:-我的 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.您将需要在 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....但你使用 ORM 我不熟悉它....

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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