[英]displaying last 30 days with data from table
我有一個記錄表,其中有一個帶有created_date的列。 我正在尋找一種按天部分中顯示的日期顯示這些記錄的方法。 因此,“今天”將僅顯示今天添加的記錄,然后僅顯示昨天的表下方的昨天記錄。 我確定它是某種數組,可能比我確定的要簡單,但我已經看過一些循環並將其拼湊在一起。 嘗試過此操作,但不確定如何使用它來填充記錄。
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編寫,所以我需要控制器,模型,視圖中的適當內容。 我認為這就是我堅持的觀點。 該模型將僅按日期檢索數據。 謝謝您的幫助!
更新時間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',
));
}
這是相關的模型
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日更新-添加了新方法
我已經將此添加到我的視圖中,它以我想要的標題形式顯示日期,但是它也打印重復項。 我希望將其歸類為以下日期:
這是視圖:
<?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
更輕松...
for( $i=1; $i<31; $i++ )
{
$the_date = date("m/d/Y", strtotime("-".$i." days"));
}
好吧,我的簡單建議是首先通過按日期過濾將數據限制在mysql本身中。 例如:created_date> = ADDDATE(CURRENT_DATE(),間隔-30天)和created_date <= CURRENT_DATE(),按created_date desc排序。
然后當您在php中檢索到它時,可以使用標記last_date =''對其進行過濾,檢查行中的當前日期是否與last_date不同,然后拾取另一個數組/組顯示並開始打印/運行。
這應該可以更輕松,更簡單地解決您的問題。
經過研究和網絡上的一些示例,我已經將此工作了。 與上述相同的模型,但是將視圖更改為:
<?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; ?>
我將不得不對布局進行一些微調,但原理是相同的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.