简体   繁体   English

使用表格中的数据显示最近30天

[英]displaying last 30 days with data from table

I have a table of records that has a column with the created_date. 我有一个记录表,其中有一个带有created_date的列。 I am looking for a way to display these records by date broke out in day sections. 我正在寻找一种按天部分中显示的日期显示这些记录的方法。 So Today would show only records that were added today and then underneath that table that shows yesterday with only yesterdays records. 因此,“今天”将仅显示今天添加的记录,然后仅显示昨天的表下方的昨天记录。 I am sure it is an array of some sort and may be simpler than i am making it out to be but i have looked at some loops and cant piece it together. 我确定它是某种数组,可能比我确定的要简单,但我已经看过一些循环并将其拼凑在一起。 Tried this but not sure how to use it to populate the records. 尝试过此操作,但不确定如何使用它来填充记录。

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

also since written in Zend i need proper stuff in the controller, model, view. 同样由于用Zend编写,所以我需要控制器,模型,视图中的适当内容。 I think the view is what i am stuck on. 我认为这就是我坚持的观点。 the model is just going to retrieve data by date. 该模型将仅按日期检索数据。 Thanks for the help! 谢谢您的帮助!

Updated 10:27 9-25-2012 This is the controller for the view 更新时间10:27 9-25-2012这是该视图的控制器

    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',
    ));
}

here are the associated models 这是相关的模型

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

Updated 09/26/2012 - Added new method 2012年9月26日更新-添加了新方法

I have added this to my view it displays the date as headers like i want however it prints duplicates also. 我已经将此添加到我的视图中,它以我想要的标题形式显示日期,但是它也打印重复项。 I am looking to group them under the date: 我希望将其归类为以下日期:

This is the 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; ?>

Here is the model i am using: Ignore previous models shown, i created a widget 这是我正在使用的模型:忽略先前显示的模型,我创建了一个小部件

    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'));
?>

Also Refer this link for such type of operations 另请参阅此链接以了解此类操作

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

Easier... 更轻松...

for( $i=1; $i<31; $i++ )
{
  $the_date = date("m/d/Y", strtotime("-".$i." days"));
}

Well my simple suggestion will be restricting the data into mysql itself first by filtering it by date. 好吧,我的简单建议是首先通过按日期过滤将数据限制在mysql本身中。 Ex: created_date >= ADDDATE(CURRENT_DATE(), INTERVAL -30 DAY) and created_date <= CURRENT_DATE(), order by created_date desc. 例如:created_date> = ADDDATE(CURRENT_DATE(),间隔-30天)和created_date <= CURRENT_DATE(),按created_date desc排序。

then when u retrieve it in php, u can filter it with the flag, last_date = '' check if the current date in the row is not the same as last_date, pickup another array / group display and start printing / functioning. 然后当您在php中检索到它时,可以使用标记last_date =''对其进行过滤,检查行中的当前日期是否与last_date不同,然后拾取另一个数组/组显示并开始打印/运行。

This should solve your issue much easier and simpler pattern. 这应该可以更轻松,更简单地解决您的问题。

After research and some examples on the web i have gotten this to work. 经过研究和网络上的一些示例,我已经将此工作了。 Same model as above but changed the view to this: 与上述相同的模型,但是将视图更改为:

<?php $prevDate = 0; ?>


<?php if ($this->sets) : ?>
<div class="t_media_list_sets">
<h2><?php echo $this->translator()->widget('title'); ?></h2>
<dl>
<?php foreach ($this->sets as $set) : ?>
<?php $date = date('y-m-d', strtotime($set->created_date)); ?>
<?php $theDate = $date ?>
<!-- Date changed -->
<?php if ($theDate != $prevDate) : ?>
    <?php if(!empty($prevDate)): ?>
    <?php  echo '</dl>'; ?>
  <?php endif; ?>
  <!-- Open new list -->
  <?php echo '<dl>'; ?>
  <!-- Show definition term for the group of events -->
  <?php echo '<dt>'; ?>
    <?php echo $theDate ?>
    <?php echo '</dt>'; ?>
<?php endif; ?>
<!-- Display current event -->
<?php echo '<dd>'; ?>
      <a href="<?php echo $this->url($set->getProperties(), 'media_set_details'); ?>">
        <?php echo $set->title; ?>
    </a>
    <?php echo $set->description; ?>
<?php echo '</dd>'; ?>

<!-- Remember date from previous row -->
<?php $prevDate = $theDate; ?>
<?php endforeach; ?>
<!-- Close last definition list -->
<?php echo '</dl>'; ?>    

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

I will have to do some fine tuning with the layout but the principal is the same. 我将不得不对布局进行一些微调,但原理是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM