我正试图找出一种更好的方式来做我想做的事情。现在在我的控制器中我有以下代码。它检查用户是否有要查看的消息,如果他们这样做,则设置表模板并设置表数据,然后显示收件箱视图。如果没有要显示的数据,则会查看用于显示任何消息的通用视图。我想做的是无论是否仍然显示某些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);
}
答案 0 :(得分: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);