我有3个表:transaction,member和mtrans。我想从事务中获取所有行。我还想从member和mtrans获取行,其中member.id = mtrans.mid
问题是,我不知道如何发送我得到的数据。这是我的代码:
控制器
function profile(){
$data["info"] = $this->itransaction_model->getData1();
$data["info"] = $this->itransaction_model->getData2();
$data["page"] = "profile";
$this->load->view("include/template.php", $data);
}
模型
function getData1(){
$this->db->from("transaction");
$query = $this->db->get();
$data['info'] = $query->result_array();
return $data;
}
function getData2(){
$this->db->from("member as m");
$this->db->join("mtrans as mt", "mt.mid = m.id");
$this->db->where("m.id", $this->uri->segment(3));
$query = $this->db->get();
$data['info'] = $query->result_array();
return $data;
}
所以,我做得对吗?如何在视图中显示?
至少发布如何使用print_r检查收集的数据。
答案 0 :(得分:0)
在模型getData1()
$this->db->select("t.*,m.*");
$this->db->from("mtrans as mt");
$this->db->join("member as m","mt.mid = m.id");
$this->db->join("transaction as t","mt.tid = t.id");
$this->db->where("m.id", $this->uri->segment(3));
$query = $this->db->get();
$res = $query->result_array();
print_r($res);
答案 1 :(得分:0)
此解决方案仅用于帮助您使用print_r打印视图中的数据。
在你的控制器中,你用你从getData2()获得的数据覆盖从函数getData1()获得的数据;
function profile(){
$data["info"] = $this->itransaction_model->getData1();
//data stored in $data["info"]
$data["info"] = $this->itransaction_model->getData2();
//data stored in $data["info"] overwritten
$data["page"] = "profile";
$this->load->view("include/template.php", $data);
}
我建议你将它们存储在两个不同的数组键中。
$data["info1"] = $this->itransaction_model->getData1();
$data["info2"] = $this->itransaction_model->getData2();
然后在您的视图中,您可以使用
print_r($info1);
print_r($info2);