Php 如何使用Zend Framework使用DATE_格式编写此查询?

Php 如何使用Zend Framework使用DATE_格式编写此查询?,php,zend-framework,zend-db-table,Php,Zend Framework,Zend Db Table,基本上,我想要一个新闻页面,根据月份和年份在侧栏中列出存档列表。。例如,2012年6月。该表名为“news”,包含news\u id、news\u title、news\u entry、updated和created列。更新和创建的是时间戳。现在我正在使用普通php,如下所示: mysql_select_db($database_admin_conn, $admin_conn); $query_getArchives = "SELECT DISTINCT DATE_FORMAT (news.up

基本上,我想要一个新闻页面,根据月份和年份在侧栏中列出存档列表。。例如,2012年6月。该表名为“news”,包含news\u id、news\u title、news\u entry、updated和created列。更新和创建的是时间戳。现在我正在使用普通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>
显示存档列的代码如下所示:

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>
新闻档案

    首先在/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;
    }
    
    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;
    }
    
    foreach($row_getArchives as $row){
        echo $row['link'];
    }
    

    @我只是喜欢使用db类,因为它们保留表名,我不需要记住它,我只需要使用
$table->info(Zend_db_table::name)`如果我想更改表名,我必须在类中更改它。这很方便。谢谢Ivan,但这给我留下了一个错误:致命错误:在第15行的C:\vhosts\balaji\news\news.php中找不到类“Application\u Model\u DbTable\u Archive”。现在阅读Zend帮助文档:)@atomheart您应该首先创建zf项目,然后创建table类。尝试更新后的答案,如果有效,接受它=)谢谢你的回复伊万。对于这个特定的项目,我只使用Zend通过library/Zend/Loader/Autoloader.php连接到数据库,并读取和写入数据库。在我的下一个学习项目中,我打算更多地参与Zend。对于我目前的需求,我想知道select语句是否可以用zend select语句格式实现。类似于:$getRecent=$dbRead->select('news\u id','news\u title')->FROM('news')->ORDER(updated)@atomheart如果不想创建table类,请使用更新的答案。