php - 显示过去 30 天的表格数据

标签 php mysql arrays zend-framework

我有一个记录表,其中有一列包含创建日期。我正在寻找一种方法来按日期部分中爆发的日期显示这些记录。因此,今天将仅显示今天添加的记录,然后在该表下方仅显示昨天的记录。我确信它是某种数组,并且可能比我想象的更简单,但我已经查看了一些循环并且无法将其拼凑在一起。尝试过此操作,但不确定如何使用它来填充记录。

for ($i = 0; $i < 30; i++)
{
$timestamp = time();
$tm = 86400 * $i; // 60 * 60 * 24 = 86400 = 1 day in seconds
$tm = $timestamp - $tm;

$the_date = date("m/d/Y", $tm);
}

而且自从用 Zend 编写以来,我需要 Controller 、模型、 View 中的适当内容。我认为观点就是我所坚持的。该模型只是要按日期检索数据。 感谢您的帮助!

2012 年 9 月 25 日 10:27 更新 这是 View 的 Controller

    public function detailsAction() 
{
    $request   = $this->getRequest();
    $setId     = $request->getParam('set_id');
    $pageIndex = $request->getParam('page_index', 1);
    $perPage = 15;
    $offset  = ($pageIndex - 1) * $perPage;

    $conn = XXX_Db_Connection::factory()->getSlaveConnection();
    $setDao  = XXX_Model_Dao_Factory::getInstance()->setModule('media')->getSetDao();
    $photoDao = XXX_Model_Dao_Factory::getInstance()->setModule('media')->getPhotoDao();
    $setDao->setDbConnection($conn);
    $photoDao->setDbConnection($conn);

    $set = $setDao->getById($setId);
    if (null == $set) {
        throw new XXX_Exception_NotFound();
    }

    /**
     * Get the list of photo that belongs to this set
     */
    $photos       = $photoDao->getPhotosInSet($setId, $offset, $perPage, true);
    $numPhotos = $photoDao->countPhotosInSet($setId, true);

    /**
     * Paginator
     */
    $paginator = new Zend_Paginator(new XXX_Utility_PaginatorAdapter($photos, $numPhotos));
    $paginator->setCurrentPageNumber($pageIndex);
    $paginator->setItemCountPerPage($perPage);

    $this->view->assign('set', $set);
    $this->view->assign('photos', $photos);

    $this->view->assign('paginator', $paginator);
    $this->view->assign('paginatorOptions', array(
        'path'     => $this->view->url($set->getProperties(), 'media_set_details'),
        'itemLink' => 'page-%d',
    ));
}

这是相关的模型

    public function getById($id) 
{
    $sql = sprintf("SELECT * FROM " . $this->_prefix . "media_set 
                    WHERE set_id = '%s'
                    LIMIT 1", 
                    mysql_real_escape_string($id));
    $rs  = mysql_query($sql);
    $return = (0 == mysql_num_rows($rs)) ? null : new Media_Models_Set(mysql_fetch_object($rs));
    mysql_free_result($rs);
    return $return;
}

    public function getPhotosInSet($setId, $offset = null, $count = null, $isActive = null)
{
    $sql = sprintf("SELECT * FROM " . $this->_prefix . "media_photo AS f
                    INNER JOIN " . $this->_prefix . "media_photo_set_assoc AS fs
                        ON fs.photo_id = f.photo_id AND fs.set_id = '%s'",
                    mysql_real_escape_string($setId));
    if (is_bool($isActive)) {
        $sql .= sprintf(" WHERE f.is_active = '%s'", (int)$isActive);
    }
    $sql .= " ORDER BY f.photo_id DESC";
    if (is_int($offset) && is_int($count)) {
        $sql .= sprintf(" LIMIT %s, %s", $offset, $count);
    }

    $rs   = mysql_query($sql);
    $rows = array();
    while ($row = mysql_fetch_object($rs)) {
        $rows[] = $row;
    }
    mysql_free_result($rs);
    return new XXX_Model_RecordSet($rows, $this);
}

public function countPhotosInSet($setId, $isActive = null)
{
    $sql = sprintf("SELECT COUNT(*) AS num_photos FROM " . $this->_prefix . "media_photo AS f
                    INNER JOIN " . $this->_prefix . "media_photo_set_assoc AS fs
                    ON fs.photo_id = f.photo_id AND fs.set_id = '%s'",
                    mysql_real_escape_string($setId));
    if (is_bool($isActive)) {
        $sql .= sprintf(" WHERE f.is_active = '%s'", (int)$isActive);
    }
    $sql .= " LIMIT 1";
    $rs   = mysql_query($sql);
    $row  = mysql_fetch_object($rs);
    mysql_free_result($rs);
    return $row->num_photos;
}

更新于 2012 年 9 月 26 日 - 添加了新方法

我已将其添加到我的 View 中,它像我想要的那样将日期显示为标题,但它也会打印重复项。我希望将它们分组在日期下:

这是 View :

<?php if ($this->sets) : ?>
<div class="t_media_list_sets">
<h2><?php echo $this->translator()->widget('title'); ?></h2>

<ul>
    <?php foreach ($this->sets as $set) : ?>
    <?php $curDate = 0; ?>
    <?php $date = $set->created_date; ?>
    <?php if ($date != $curDate) : ?>
        <?php $curDate = $date; ?>
        <?php echo  $curDate; ?>
    <?php endif; ?>

    <li>
        <a href="<?php echo $this->url($set->getProperties(), 'media_set_details'); ?>">
            <?php echo $set->title; ?>
        </a>
        <?php echo $set->description; ?>
    </li>
    <?php endforeach; ?>
</ul>

<div class="clearfix"></div>
</div>
<?php endif; ?>

这是我正在使用的模型:忽略以前显示的模型,我创建了一个小部件

    public function setlist($offset = null, $count = null, $exp = null) 
{
    $sql = "SELECT * FROM " . $this->_prefix . "media_set AS s";
    if ($exp) {
        $where = array();

        if (isset($exp['created_user_id'])) {
            $where[] = sprintf("s.created_user_id = '%s'", mysql_real_escape_string($exp['created_user_id']));
        }
        if (isset($exp['keyword'])) {
            $where[] = "s.title LIKE '%" . addslashes($exp['keyword']) . "%'";
        }
        if (isset($exp['is_active'])) {
            $where[] = sprintf("s.is_active = '%s'", (int)$exp['is_active']);
        }

        if (count($where) > 0) {
            $sql .= " WHERE " . implode(" AND ", $where);
        }
    }
    $sql .= " ORDER BY s.created_date DESC";
    if (is_int($offset) && is_int($count)) {
        $sql .= sprintf(" LIMIT %s, %s", $offset, $count);
    }

    $rs   = mysql_query($sql);
    $rows = array();
    while ($row = mysql_fetch_object($rs)) {
        $rows[] = $row;
    }
    mysql_free_result($rs);
    return new XXX_Model_RecordSet($rows, $this);
}

最佳答案

<?php    
$d = array();
for($i = 0; $i < 30; $i++) 
    $d[] = date("d", strtotime('-'. $i .' days'));
?>

另请参阅此链接以了解此类类型的操作

http://www.vision.to/how-to-add-days-weeks-months-to-any-date-.php

关于php - 显示过去 30 天的表格数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12593668/

相关文章:

PHP-将 $_POST 保存到 $_SESSION 问题

php - mysqli_fetch_assoc()需要参数/调用成员函数bind_param()错误。如何获取并修复实际的mysql错误?

php - 无法在表中显示数据

jquery - 无法将对象作为参数传递

php - CakePHP 返回 find ('all' ) 使用 'id' 作为数组索引

php - 如何从 LDAP 获取用户名?

PHP 和 MySQL 最佳实践——为管理员和用户创建不同的 View

mysql - 如何找到不同的组合?

java - 将字符串拆分为数组的 n 个长度元素

javascript - 了解 'arr.filter(callback[, thisArg])' 语法