2013-08-31 107 views
0

我正在尝试了解如何向用户显示一条消息,通知他们从他们尝试从数据库中删除内容页面时出现错误或成功消息。我想知道如果我“在做正确为止。如果我是什么什么,我的看法吗?在Codeigniter中显示来自flashdata的成功/失败消息

控制器

/** 
* Content_pages::delete_content_page() 
* 
* Deletes a content page from the list of content pages. 
* 
* @param string $content_page_id The id of the content page being deleted. 
* @return void 
*/ 
public function delete_content_page($content_page_id) 
{ 
    $status = 'unprocessed'; 
    $title = 'Action Unprocessed'; 
    $message = 'The last action was rendered unprocessed. Please try again.'; 
    if (isset($content_page_id) && is_numeric($content_page_id)) 
    { 
     $content_page_data = $this->content_page->get($content_page_id); 
     if (!empty($content_page_data)) 
     { 
      $this->content_page->update($content_page_id, array('status_id' => 3)); 
      if ($this->db->affected_rows() > 0) 
      { 
       $status = 'success'; 
       $message = 'The content page has been successfully deleted.'; 
       $title = 'Content Page Deleted'; 
      } 
      else 
      { 
       $status = 'error'; 
       $message = 'The content page was not deleted successfully.'; 
       $title = 'Content Page Not Deleted'; 
      } 
     } 
    } 
    $output = array('status' => $status, 'message' => $message, 'title' => $title); 
    $this->session->set_flashdata('output', $output); 
    redirect('content-pages/list'); 
} 

/** 
* Content_pages::list_content_pages() 
* 
* List all of the content pages found in the database. 
* 
* @return void 
*/ 
public function list_content_pages() 
{ 
    $content_pages = $this->content_page->get_all(); 

    $data['output'] = $this->session->flashdata('output'); 

    $this->template 
     ->title('Content Pages') 
     ->set('content_pages', $content_pages) 
     ->build('content_pages_view', $data);  
} 

我的问题是在视图,因为它显示为默认为空消息,所以我试图找出如何不显示它时,认为首先呈现,只有当存在要显示的消息。

if (isset($output)) 
{ 
    if ($output['status'] == 'success') 
    { 
     echo '<div class="alert alert-success">'; 
    } 
    elseif ($output['status'] == 'error') 
    { 
     echo '<div class="alert alert-error">'; 
    } 
    else 
    { 
     echo '<div class="alert alert-error">'; 
    } 

    echo '<button type="button" class="close" data-dismiss="alert">&times;</button>'; 
    echo '<strong>' . $output['title'] . '</strong>' . $output['message']; 
    echo '</div>'; 
} 
?> 
+0

在视图中,您只需执行'echo $ output'。 – rgin

回答

0

我已经设置了默认的会话闪存数据值,当第一次加载的时候,在这种情况下,如果没有设置数据,它会返回什么样的值。所以我需要添加一个条件来检查值是否为假。

1

我不知道您的自定义模板类不具有->title()->set()->build()什么。但它看起来像你仍然通过$data进入你的视图。

因此,您只需在您的视图中执行echo $output;

编辑:

我认为对于$output仍然显示的原因是因为这些代码并不是if语句里面:

echo '<button type="button" class="close" data-dismiss="alert">&times;</button>'; 
echo '<strong>' . $output['title'] . '</strong>' . $output['message']; 
echo '</div>'; 

尝试移动他们的if语句里面的地方只有在设置了$output时才会打印出来。

+0

谢谢,不过我想指出我的视图代码。我只显示了if语句来检查要显示的消息,但它总是显示在视图渲染中,这不应该发生。我应该在if语句中添加什么作为支票。 – user2576961

+0

检查我编辑的答案。 – rgin

+0

如果您发现它们在if语句中。 – user2576961

相关问题