简体   繁体   English

我怎样才能将此查询传递给codeigniter中的控制器,并在我的foreach中使用它

[英]how can i pass this query to my controller in codeigniter and to use this in my foreach in view

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

} }

Message: Invalid argument supplied for foreach() -> having this error 消息:为foreach()提供了无效的参数->出现此错误

With the given information let me answer the question. 使用给定的信息,让我回答问题。

how can i pass this query to my controller in CodeIgniter

Understand that query isn't passed to the controller from the model but what's passed is data. 了解查询不是从模型传递到控制器的,而是传递的是数据。 what you have to do is returned the data from model to controller and set the data into the $data variable in the controller which is then passed to the view. 您要做的是将数据从模型返回到控制器,并将数据设置到控制器中的$data变量中,然后将其传递到视图。

Adding more you are getting above error because there is no data(result array is empty) in the array. 添加更多,您会遇到错误,因为该数组中没有数据(结果数组为空)。 Make sure that you query returns array of data as well. 确保查询也返回数据数组。 Try executing it manually in MySQL and see the result. 尝试在MySQL中手动执行它,然后查看结果。

Try result_array() which returns the query result as a pure array, or an empty array when no result is produced. 尝试使用result_array() ,该查询以纯数组形式返回查询结果,或者在没有结果产生时返回一个空数组。

Read more 阅读更多

First try execute your query manuly, and check get any result you can try your program. 首先尝试手动执行查询,然后检查是否有任何结果可以尝试您的程序。 Invalid argument supplied for foreach() -> having this error this type of error occurred your result array may be empty. Invalid argument supplied for foreach() -> having this error ,您的结果数组可能为空。

first create a model . 首先创建一个模型。 and paste this code on model like your model name is ex_model.php 并将此代码粘贴到模型上,例如您的模型名称为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;
}

and create controller and get add this code like 并创建控制器并添加如下代码

<?php
public function reffers(){

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

now create a view name reffer.php and get all data from foreach loop like 现在创建一个视图名称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
}

?>

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

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