首先是一些代码,我的帖子末尾的问题。我的代码从 DB 生成列表 li ul:
示例 SQL:
CREATE TABLE IF NOT EXISTS `gallery` (
`idgallery` int(11) NOT NULL,
`name` varchar(45) NOT NULL,
`parent` varchar(45) DEFAULT '0',
`type` varchar(45) DEFAULT 'category',
`menu` varchar(45) NOT NULL,
PRIMARY KEY (`idgallery`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `gallery` (`idgallery`, `name`, `parent`, `type`, `menu`) VALUES
(1, 'Gallery', '0', 'Gallery', ''),
(2, 'Name', '1', 'category', ''),
(3, 'Name2', '0', 'category', ''),
(4, 'Name3', '3', 'category', ''),
(5, 'qwerty', '4', 'category', ''),
(6, 'child', '3', 'category', ''),
(7, 'child', '1', 'category', '');
此代码在迁移到 ZendFW 之前运行良好:
$ro=mysql_query("SELECT * FROM gallery");
$rows=array();
while($rowek=mysql_fetch_object($ro))
{
$rows[]=$rowek;
}
echo mapTree($rows,0,"category");
function children($rows, $id) {
foreach ($rows as $row) {
if ($row->parent == $id)
return true;
}
return false;
}
function mapTree($rows, $parent = 0, $class = "none") {
$result = "<ul>";
foreach ($rows as $row) {
if ($row->parent == $parent) {
$result.= "<li class='$class'>$row->name";
if (children($rows, $row->idgallery))
$result.= mapTree($rows, $row->idgallery, "category");
$result.= "</li> ";
}
}
$result.= "</ul>";
return $result;
}
在 Zend FW 中,我使用的是 Action Helper。*所有对象数据库都传递给我的助手(error_log),但它只显示:*
Gallery
Name
两条记录良好的秩序/样式......但是另外 5 条在哪里?
索引操作:
//preDispatch()
Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH .'/controllers/helpers');
//IndexAction()
$gallery = new Application_Model_DbTable_Gallery;
$request = $gallery->fetchAll($gallery->select()->order("parent"));
$helper = $this->_helper->gallery->index($request);
$this->view->gallery = $helper;
动作助手
class Zend_Controller_Action_Helper_Gallery extends Zend_Controller_Action_Helper_Abstract{
function index($rows){
echo $this->mapTree($rows,0,"");
}
function children($rows, $id) {
foreach ($rows as $row) {
if ($row->parent == $id)
return true;
}
return false;
}
function mapTree($rows, $parent = 0, $class = "brak") {
$result = "<ul>";
foreach ($rows as $row) {
if ($row->parent == $parent) {
$result.= "<li class='$class'>$row->name";
if ($this->children($rows, $row->idgallery))
$result.= $this->mapTree($rows, $row->idgallery, "category");
$result.= "</li> ";
}
}
$result.= "</ul>";
return $result;
}
}
并且还查看...
<?php $this->gallery;?>
我的问题是 - 为什么我在 ZF 中的代码无法完成?