Codeigniter - 在将数据库发送到视图之前处理从DB中检索到的值

时间:2012-08-13 12:49:23

标签: php mysql database codeigniter-2

我想通过使用php函数date()来显示从DB捕获的时间戳值,以正确设置日期时间格式。

但问题是 - 可以在将其传递给视图文件之前完成吗?
我希望我的视图文件尽可能保持视图文件中的php函数。

使用/models/content_model.php检索数据库中的项目:

function getPages($per_page){
    $this->db->order_by('id', 'desc');
    $query = $this->db->get('content', $per_page, $this->uri->segment(3));
    return $query->result();
}

然后,它由控制器/controllers/content.php处理:

...
// Get data
$componentData['results'] = $this->content_model->getPages(5);
...

并在views/content/home.php显示结果:

<?php
    <?php foreach($results as $item):?>
        echo $item->date_created;
     <?php endforeach;?>
?>

将输出日期(在DB中键入时间戳),如下所示:2011-08-14 18:34:04

我不想在视图文件date()中使用views/content/home.php函数,而是想通过视图外的date()函数来处理它,不确定它是否应该在模型中完成或控制器。

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您可以在控制器类中执行此操作。首先创建一个私有函数名称DateProcess并发送查询结果并根据需要处理日期。然后,您可以将过程数据发送到查看文件。