如果查询返回true,则返回模型中的变量 [英] Return a variable in my model if the query returns true
问题描述
$q = $this->db->get_where('person', array('p_id' => $p));
if($q->num_rows()!=1)
{
redirect('General_Area/Home');
exit();
}
else
{
. . .
Ok所以一旦模型初始化,它查询db并查找一个匹配,如果找到它在else语句上移动。但是如果没有找到,它会 redirect('General_Area / Home');
Ok So once the model is initialized it queries the db and looks for exactly one match and if found it moves on the else statement. However if not found it will redirect('General_Area/Home');
那里?在我的控制器中,如果查询成功,则返回对象
。
How do I pass a message in there? In my controller I am returning an object
if the query is successful.
在我看来,我是 echo obj-> table_col_name
$q = $this->db->get_where('person', array('p_id' => $p));
if($q->num_rows()!=1)
{
return $Error = 'You have not been found!...';
#redirect('General_Area/Home');
exit();
}
else
{
. . .
如果 $ q
希望能够在视图中为<$ p $ c> echo $ error; 查看该消息。
If the $q
was not successful I want to be able to echo $error;
in the view for the user to see the message.
推荐答案
在您的模型中
In your Model
if($q->num_rows()>0)
{
return array('result'=>$q->result(), 'message'=>'This is a message');
}
return false;
在控制器
In the Controller
$this->load->model('your_model_name');
$data['query']=$this->your_model_name->model_function_name();
if(!$data['query']['result'])
{
redirect('General_Area/Home');
exit();
}
else $this->load->view('your_view_name',$data);
在您的检视中
In your View
if(isset($query))
{
foreach($query as $row)
{
// code goes here to echo columns
}
//and message is available as $message so you can print it like
if(isset($message)) echo $message;
}
重新导向讯息
也可以在重定向到您可以在控制器中使用的其他页面时发送消息
Message on redirect
Also if you want to send a message when you redirect to another page you can use in your controller
if(!$data['query']['result'])
{
$this->session->set_flashdata('message', 'your message text here!');
redirect('General_Area/Home');
exit();
}
因此,您可以在视图中打印邮件
So you can print the message in the view like
echo $this->session->flashdata('message');
详细了解 Flashdata 。
这篇关于如果查询返回true,则返回模型中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!