我刚开始使用Codeigniter,这让我感到非常紧张。我有一个查询来确定用户是否购买了任何程序。然后,我必须使用该程序的类型类别来运行并确定他或她在另一个表中记录了多少次查询。对于混淆抱歉,但代码希望是有道理的。Codeigniter的多个/子查询
我有问题将两个数组从我的模型返回到我的控制器到显然视图上。
function specificPrograms() {
$specific_sql = $this->db->query("SELECT program,created FROM `assessment` WHERE uid = $this->uid");
if($specific_sql->num_rows() > 0) {
foreach ($specific_sql->result() as $specific) {
$data[] = $specific;
$this->type = $specific->program;
}
return $data;
}
$sub_sql = $this->db->query("SELECT id FROM othertable WHERE user_id_fk = $this->uid and type = '$this->type'");
if($sub_sql->num_rows() > 0) {
foreach ($sub_sql->result() as $otherp) {
$data[] = $otherp;
}
return $data;
}
}
然后在我的控制器我有,
$data['specific'] = $this->user_model->specificPrograms();
$data['otherp'] = $this->user_model->specificPrograms();
感谢您的帮助。
您能描述一下您对代码的期望吗?在运行之后,你希望'$ data ['specific']'和'$ data ['otherp']'被设置为什么?他们现在做了什么? – 2012-07-09 03:18:13
当然,基本上。假设用户购买了3个独立的程序,并且对于每个程序,他或她已经使用了它一定次数,这个数字将被记录为来自其他人的ID。例如,Bob购买了产品1,产品2和产品3.我需要知道Bob购买的“程序”以及与其产品或类型相对应的其他ID是否已记录在其他产品或类型中。这有任何意义吗? – user1011713 2012-07-09 03:21:31
对不起,让我们试试更好的例子:鲍勃购买了3种不同的锻炼:棒球,篮球和足球锻炼。对于每个人都已经完成了一定数量的锻炼,这些锻炼将被记录并使用第二个sql查询返回的num-rows。我需要这项运动(篮球,足球,棒球......)以及鲍勃为每项运动所做的锻炼次数(计数ID)。 – user1011713 2012-07-09 03:23:26