Reputation: 415
I am trying learn how to display a message to the user informing them of either an error or success message from them attempting to delete a content page from the database. I'm wondering if I"m doing it right so far. If I am what what I do in the view?
Controller
/**
* 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);
}
My issue is in the view because it shows up as default empty message so I'm trying to figure out how to not show it when the view first renders and only when there is a message to be displayed.
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">×</button>';
echo '<strong>' . $output['title'] . '</strong>' . $output['message'];
echo '</div>';
}
?>
Upvotes: 0
Views: 4247
Reputation: 415
I had set the default as the session flash data value which when first loaded would be false in this case because of what it returns if no data is set. So I needed to add a condition to check if the value was false.
Upvotes: 0
Reputation: 2311
I'm not sure what your custom template class does with ->title()->set()->build()
. But it does look like you're still passing $data
into your view.
So, you simply do echo $output;
in your view.
EDIT:
I think the reason for the $output
still showing up is because these lines of code aren't inside the if statement:
echo '<button type="button" class="close" data-dismiss="alert">×</button>';
echo '<strong>' . $output['title'] . '</strong>' . $output['message'];
echo '</div>';
Try moving them inside the if statement where it will only be printed out if $output
is set.
Upvotes: 1