user2576961
user2576961

Reputation: 415

Attempting to display message with no results

What I"m trying to understand how to do is display a message to the user when there are no results returned from the database query. As of right now I have the query working correctly. In the case there that the count of the messages variable is 0 then what I'd like to do is display a message saying there are no personal messages in the outbox. This includes not showing the table.

What I have thought about doing is moving the lines that set the object properties to above the code below and then setting two separate build methods. One for when there's results and one when there isn't. Then in the chance there isn't results I'd have it build a view that is blank that just says a random message.

For a template library I am currently using Phil Sturgeon's template library.

https://github.com/jeffreydavidson/codeigniter-template

I'm seeking some better possibilities with this. Anybody available to assist?

$messages = $this->messages->get_many_by('sender_id', $this->session->userdata('user_id')); 
//vardump($messages); die();

if (count($messages > 0))
{
    $tmpl = array('table_open' => '<table class="table table-bordered table-condensed table-striped table-vertical-center checkboxs js-table-sortable">', 'row_start' => '<tr class="selectable">'); 
    $this->table->set_template($tmpl); 
    $this->table->set_heading('', 'To', 'Subject', 'Date', 'Actions');

    foreach ($messages AS $message)
    {
        $this->table->add_row('', $message->receiver_id, $message->subject, date('F d, Y', strtotime($message->date_sent)), '<a href="'. site_url() .'wrestling-manager/personal-messages/sent/delete/' . $message->id .'" class="btn-action glyphicons remove_2 btn-danger"><i></i></a>');
    }
}
else
{
    echo 'test';
}

$this->template
    ->title('Personal Messages Sent')->set_layout('control_panel_view')
    ->set_partial('sidebar', 'partials/sidebar')
    ->set('user_data', $this->user->with('character')->get($this->session->userdata('user_id')))
    ->build('sent_view');

Upvotes: 0

Views: 80

Answers (2)

cartalot
cartalot

Reputation: 3148

Suggest that you first get your application working without the template library - that will enforce the separation that you need. Your controller manages the flow of the application decisions but should have little information about display. So if there is a query for Personal Messages

  • controller receives request to search for personal messages
  • search input is filtered / search performed / results returned
  • the controller says - if there are no results - then go to the no results method. maybe the controller is also passing a data structure that has the search term or other relevant info for the user.

How that result is displayed - is kept separate as much as possible. so even if there are some template specific methods in the controller they will be clearly separated. the payoff: quick and easy to change the design, templates, etc.

Upvotes: 2

Jim
Jim

Reputation: 18853

if (count($messages > 0)) should be: if (count($messages) > 0)

Not sure if that will fix your issue(s), but it will be a start in the right direction.

Upvotes: 2

Related Questions