2

我试图找出一种更好的方法来做我想做的事情。现在在我的控制器中,我有以下代码。它会检查用户是否有任何消息要查看,如果有的话,它会设置表格模板并设置表格数据,然后显示收件箱视图。如果没有要显示的数据,它会查看用于显示任何消息的通用视图。我想做的是无论是否有数据仍然显示一些html。我想显示收件箱链接、创建消息链接等。这样做的更好方法是什么?

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);
}
4

1 回答 1

1

我不确定您要做什么更好if()但下面是一个选项,无论是否有消息,所有设置在语句上方的数据都将被传递。

$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);
于 2013-08-12T16:58:03.827 回答