我如何将此查询传递给codeigniter中的控制器,并在我的foreach视图中使用它

时间:2015-04-03 05:38:51

标签: php codeigniter

function getAllReferrals(){

$sql = "(SELECT r.referral_date,c.lastname,c.middlename,c.firstname,c.gender,r.presenting_problem,e.employee_nickname 
    AS nickname FROM CLIENT c INNER JOIN referral1 r ON c.referral_id = r.referral1_id INNER JOIN assign_psychotherapist ap 
    ON ap.a_referral_id = c.referral_id INNER JOIN employee e ON ap.a_psychotherapist_id = e.empid WHERE r.referral_status ='Assigned' 
    OR r.referral_status ='Accepted' ORDER BY referral_date DESC ) UNION ALL (SELECT r.referral_date,c.lastname,c.middlename,c.firstname,
    c.gender,r.presenting_problem,v.volunteer_nickname AS nickname FROM CLIENT c INNER JOIN referral1 r ON c.referral_id = r.referral1_id 
    INNER JOIN assignvolunteer av ON av.Vreferralid = c.referral_id INNER JOIN volunteer v ON av.Vvolunteerid = v.volid 
    WHERE r.referral_status ='Assigned' OR r.referral_status ='Accepted' ORDER BY referral_date DESC )";

$query = $this->db->query($sql);
if ($query->num_rows() > 0){
    return $query->result();
}else{
    return NULL;
}

}

  

消息:为foreach()提供的参数无效 - >有这个错误

3 个答案:

答案 0 :(得分:0)

根据给定的信息,让我回答这个问题。

how can i pass this query to my controller in CodeIgniter

了解查询未从模型传递给控制器​​,但传递的是数据。您需要做的是将数据从模型返回到控制器,并将数据设置为控制器中的$data变量,然后传递给视图。

由于数组中没有数据(结果数组为空),因此添加更多错误。确保您查询也返回数据数组。尝试在MySQL中手动执行它并查看结果。

尝试result_array()将查询结果作为纯数组返回,或者在没有结果时返回空数组。

Read more

答案 1 :(得分:0)

首先尝试手动执行您的查询,并检查获得任何可以尝试您的程序的结果。 Invalid argument supplied for foreach() -> having this error发生此类错误,您的结果数组可能为空。

答案 2 :(得分:0)

首先创建一个模型。并将此代码粘贴到模型上,就像您的模型名称是ex_model.php

一样
    function getAllReferrals(){

$sql = "(SELECT r.referral_date,c.lastname,c.middlename,c.firstname,c.gender,r.presenting_problem,e.employee_nickname 
    AS nickname FROM CLIENT c INNER JOIN referral1 r ON c.referral_id = r.referral1_id INNER JOIN assign_psychotherapist ap 
    ON ap.a_referral_id = c.referral_id INNER JOIN employee e ON ap.a_psychotherapist_id = e.empid WHERE r.referral_status ='Assigned' 
    OR r.referral_status ='Accepted' ORDER BY referral_date DESC ) UNION ALL (SELECT r.referral_date,c.lastname,c.middlename,c.firstname,
    c.gender,r.presenting_problem,v.volunteer_nickname AS nickname FROM CLIENT c INNER JOIN referral1 r ON c.referral_id = r.referral1_id 
    INNER JOIN assignvolunteer av ON av.Vreferralid = c.referral_id INNER JOIN volunteer v ON av.Vvolunteerid = v.volid 
    WHERE r.referral_status ='Assigned' OR r.referral_status ='Accepted' ORDER BY referral_date DESC )";

$query = $this->db->query($sql);
if ($query->num_rows() > 0){
    return $query->result();
}else{
    return NULL;
}

并创建控制器并添加此代码,如

<?php
public function reffers(){

$this->load->model('ex_model');
$data['refferdata'] = $this->ex_model->getAllReferrals();
$this->load->view('reffer');
}
?>

现在创建一个视图名称reffer.php并从foreach循环中获取所有数据,如

<?php

foreach($refferdata as $r)
{
echo $r->r.referral_date.<br>;
echo $r->c.lastname.<br>;

// and so on like this in view
}

?>