简体   繁体   中英

displaying last 30 days with data from table

I have a table of records that has a column with the 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. 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

    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

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

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. Ex: created_date >= ADDDATE(CURRENT_DATE(), INTERVAL -30 DAY) and created_date <= CURRENT_DATE(), order by 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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