[英]how to write this query using DATE_FORMAT using Zend Framework?
基本上,我希望有一個新聞頁面,該新聞頁面的月份和年份基於側面列中的“存檔”列表。例如2012年6月。該表名為“ news”,其中列news_id,news_title,news_entry,已更新,已創建。 更新和創建的是時間戳。 現在我正在使用普通的PHP,如下所示:
mysql_select_db($database_admin_conn, $admin_conn);
$query_getArchives = "SELECT DISTINCT DATE_FORMAT (news.updated, '%M %Y') AS archive, DATE_FORMAT (news.updated, '%Y-%m') AS link FROM news ORDER BY news.updated DESC";
$getArchives = mysql_query($query_getArchives, $admin_conn) or die(mysql_error());
$row_getArchives = mysql_fetch_assoc($getArchives);
$totalRows_getArchives = mysql_num_rows($getArchives);
顯示檔案列的代碼如下:
<h3>News Archives</h3>
<ul>
<?php do { ?>
<li class="seperator
<?php echo (isset($_GET['archive']) && $_GET['archive'] == $row_getArchives['link'] ? 'currentItem' : '') ?>">
<a href="news.php?archive=<?php echo $row_getArchives['link']; ?>"><?php echo $row_getArchives['archive']; ?></a>
</li>
<?php } while ($row_getArchives = mysql_fetch_assoc($getArchives)); ?>
</ul>
如何在Zend Framework的第一段代碼中實現select語句,以實現與當前代碼輸出相同的結果? 提前致謝!
首先在/models/DbTable/Archive.php中創建表類,或使用zf命令工具。
class Application_Model_DbTable_Archive extends Zend_Db_Table_Abstract
{
protected $_name = 'news'; // actual table name here
}
然后使用以下代碼
$table = new Application_Model_DbTable_Archive();
$select = $table->getAdapter()->select()
->from(array('news' => $table->info(Zend_Db_Table::NAME)),
array(
'archive'=>new Zend_Db_Expr('DISTINCT DATE_FORMAT (news.updated, \'%M %Y\')'),
'link'=>new Zend_Db_Expr('DISTINCT DATE_FORMAT (news.updated, \'%Y-%m\')')))
->order('news.updated DESC');
// then use $select
$row_getArchives = $table->fetchAll($select);
$totalRows_getArchives = $row_getArchives ->count();
和視圖
foreach($row_getArchives as $row){
echo $row->link;
}
如果您不想使用表類,請嘗試以下一種:
$adapter = Zend_Db_Table::getDefaultAdapter();
$select = $adapter->select()
->from(array('news' => 'news'),
array(
'archive'=>new Zend_Db_Expr('DISTINCT DATE_FORMAT (news.updated, \'%M %Y\')'),
'link'=>new Zend_Db_Expr('DISTINCT DATE_FORMAT (news.updated, \'%Y-%m\')')))
->order('news.updated DESC');
// then use $select
$row_getArchives = $adapter->fetchAll($select);
$totalRows_getArchives = count($row_getArchives);
和視圖
foreach($row_getArchives as $row){
echo $row['link'];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.