迭代Zend_Paginator对象以进行显示

时间:2012-03-17 22:59:58

标签: php zend-framework

我有一个来自此查询的Zend_Paginator_Adapter_DbTableSelect对象:

public function fetchPagedTracks() {

        $select = $this->select(Zend_Db_Table::SELECT_WITH_FROM_PART)
                       ->setIntegrityCheck(FALSE);
        $select->join(array('ar' => 'artist'), 'ar.id = track.artist_id',
                array('artist_name' => 'name'));
        $select->join(array('al' => 'album'), 'al.id = track.album_id',
                array('album_name' => 'name', 'artist_id', 'year'));
        $select->order('artist_name', 'ASC');
        $select->order('album_name');
        $select->order('track', 'ASC');

        //create a new instance of the paginator adapter and return it
        $adapter = new Zend_Paginator_Adapter_DbTableSelect($select);

        return $adapter;
    }

数据按预期返回并且有效,我的问题在于我的观点。我想以特定的方式迭代结果集。基本上我想让艺术家位于一张桌子的顶部,每张专辑都有子表格,每张专辑都按顺序列出了曲目。我已经完成了这个......但是不正确,我必须做我认为是黑客的事情,我希望能帮助我解决它。

以下是观点:

<?php
echo $this->paginationControl(
        $this->paginator, 'Sliding', '_paginatorControl.phtml'
)
?>
<table>
    <?php
    foreach ($this->paginator as $track):;
//     Zend_Debug::dump($track, '');
        $artist = $track->artist_name;
        $album = $track->album_name;
        ?>
        <?php if ($end_artist != $artist): ?>
            <tr>
                <th colspan="4"><?php echo $track->artist_name ?></th>
            </tr>
        <?php endif ?>
        <?php if ($end_album != $album): ?>
            <tr>
                <th colspan="4"><?php echo $track->album_name . ' ' . $track->year ?></th>
            </tr>

        <tr>
            <th>Track #</th>
            <th>Title</th>
            <th>Bitrate</th>
            <th>Play Time</th>
        </tr>
        <?php endif; ?>
        <tr>
            <td><?php echo $track->track ?></td>
            <td><?php echo $track->title ?></td>
            <td><?php echo $track->bit_rate . 'Kbps' ?></td>
            <td><?php echo $this->playtime($track->play_time) ?></td>
        </tr>
        <?php
        $end_artist = $track->artist_name;
        $end_album = $track->album_name
        ?>

    <?php endforeach ?>
</table>
<?php
echo $this->paginationControl(
        $this->paginator, 'Sliding', '_paginatorControl.phtml'
); ?>

最大的问题是此代码会导致两个注意事项:

  

注意:未定义的变量:end_artist in   E:\ WWW \ home.local \程序\模块\音乐\意见\脚本\首页\ display.phtml   在第13行......和第18行

有问题的变量是$end_artist$end_album,对我而言,我无法弄清楚如何以不同的方式做到这一点。建议?

P.S。可以在Pastbin

看到paginator对象的转储

1 个答案:

答案 0 :(得分:1)

似乎$end_artist$end_album变量之前未定义,put

$end_artist = '';
$end_album = '';

之前foreach