[英]In Codeigniter, get the count of rows that including an ID number which is primary key in another table
I have two tables, first ones name is organizations like this: 我有两个表,第一个是这样的组织:
ID | ID | org_name
org_name
1 - Congress1 1-国会1
2 - Congress2 2-国会2
3 - Congress3 3-国会3
4 - Congress4 4-国会4
And the other table is visitors like this: 另一个表是这样的访客:
ID | ID | visitors |
访客| organizationID
机构编号
1 - W. Rooney - 1 1-鲁尼-1
2 - S. Aguero - 1 2-S.Aguero-1
3 - C. Ronaldo - 2 3-罗纳尔多-2
4 - L. Messi - 4 4-梅西(L. Messi)-4
My problem is:For every organizations ID, how can I count the rows that including organizationsID in visitors table like that: 我的问题是:对于每个组织ID,我如何计算来访者表中包含organizationsID的行,如下所示:
organizationsID=1 > row count in visitors=2 组织ID = 1>访问者中的行数= 2
organizationsID=2 > row count in visitors=1 组织ID = 2>访问者中的行数= 1
organizationsID=3 > row count in visitors=0 组织ID = 3>访问者中的行数= 0
organizationsID=4 > row count in visitors=1 组织ID = 4>访问者中的行数= 1
And that problem is in Codeigniter framework. 这个问题在Codeigniter框架中。 My model function:
我的模型功能:
$dbase->select('*');
$dbase->from('organizations');
$dbase->order_by("org_startdate", "desc");
$query = $dbase->get();
My controller function: 我的控制器功能:
$this->load->model('organizations_m');
$data['organizations'] = $this->organizations_m->organizations();
$this->load->view('organizations_v', $data);
And finally in my view i list my organizations: 最后,在我看来,我列出了我的组织:
foreach ($organizations as $org){
echo "Organization Name:".$org->org_name;
echo "Count of visitors that joining to this Organization".MY PROBLEM IS HERE;
}
My final result should be shown in the browser like that: 我的最终结果应显示在浏览器中,如下所示:
Organization Name: Congress1 组织名称:Congress1
Count of visi... : 2 可见...:2
Organization Name: Congress2 组织名称:Congress2
Count of visi... : 1 可见计数:1
Organization Name: Congress3 组织名称:Congress3
Count of visi... : 0 可见计数:0
Organization Name: Congress4 组织名称:Congress4
Count of visi... : 1 可见计数:1
Thank you for your replies.. 谢谢您的回复..
In your model: 在您的模型中:
$dbase->select('o.org_name,COUNT(v.id) AS no_visitors', FALSE);
$dbase->from('organizations o');
$dbase->join('visitors v', 'o.id = v.organizationID', 'left');
$dbase->group_by('o.id');
$dbase->order_by("org_startdate", "desc");
$query = $dbase->get();
Then in your view 然后在您看来
foreach ($organizations as $org){
echo "Organization Name:".$org->org_name;
echo "Count of visitors that joining to this Organization".$org->no_visitors;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.