2

I'm trying to figure out a better way of doing what it is I'm wanting to do. Right now in my controller I have the following code. It checks to see if the user has any messages to view and if they do it sets the table template and sets the table data and then displays the inbox view. If there was not data to display it views a generic view for displaying any message. What I would like to do is no matter what still display some html regardless if there is data or not. I want to display the inbox link, create a message link, etc. Better way of doing this?

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(form_checkbox(), 'From', 'Subject', 'Date', 'Actions');

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

    $this->template->build('inbox_view');
}
else
{
    $data = array('message' => 'There are no messages in your inbox folder.');
    $this->template->build('general_view', $data);
}

1 Answer 1

0

I am not sure what you are trying to do that is better. But below is an option and all data will be passed that is set above the if() statement regardless if there are messages or not.

$data['inbox_link'] = 'http://...';
$data['create_message_link'] = 'http://...';

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(form_checkbox(), 'From', 'Subject', 'Date', 'Actions');

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

    $view = 'inbox_view';
}
else
{
    $view = 'general_view';
    $data = array('message' => 'There are no messages in your inbox folder.');
}

$this->template->build($view, $data);
Sign up to request clarification or add additional context in comments.

4 Comments

I like it however my question is what about needing the additional buttons regardless if there's table data or not.
I updated my answer. I was re-reading your question and realized that. Notice the $data above the if statements.
As much as I love this answer and think its a very viable solution I also thought of loading a view inside another view and put that HTML inside there.
You could do that too, I have done that before, but usually to follow MVC patterns, though, you would want to control this from the controller as much as possible which will allow your app to be more dynamic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.