1

模型

  $q = $this->db->get_where('person', array('p_id' => $p));
  if($q->num_rows()!=1) 
    {
    redirect('General_Area/Home');
    exit();
    }
    else
    {   
         . . . 

好的 因此,一旦模型被初始化,它就会查询数据库并查找恰好一个匹配项,如果找到它就会在 else 语句上移动。但是,如果没有找到它会redirect('General_Area/Home');

我如何在那里传递消息?object在我的控制器中,如果查询成功,我将返回一个。

在我看来,我是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没有成功,我希望能够echo $error;在视图中让用户看到消息。

4

1 回答 1

1

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');

Read more about Flashdata.

于 2012-10-15T01:37:51.327 回答