0

最初にいくつかのコード、私の投稿の最後に質問。私のコードはDBからリストliulを生成します:

サンプル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;
}

そして、ZendFWではActionHelperを使用しています。*すべてのオブジェクトDBがヘルパー(error_log)に渡されますが、表示されているのは次のとおりです。*

Gallery
      Name

順調/スタイリングの2つのレコード...しかし、別の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のコードを完成できないのかということです。

4

1 に答える 1

0

Two records in good order/styling... but where is another 5?

Your function did what you told it to and processed just those rows with the parent of '0':
if ($row->parent == $parent) and echo $this->mapTree($rows,0,"");

If you intend for '0' to mean false or null, as it appears you do, you may wish to rethink how you implement that piece of functionality.

Zend Framework のビュー ヘルパーと同様に、アクション ヘルパーは値が返されたときに最適に機能するようです。この場合、毎回direct()呼び出す必要がないように実装することもできます。->index()

class Zend_Controller_Action_Helper_Gallery extends Zend_Controller_Action_Helper_Abstract
{    
    //we're using php 5, visibility matters
    public function direct($rows){
        return $this->mapTree($rows,0,"");
    }
    protected function children($rows, $id) {
    //truncated
    }
    protected function mapTree($rows, $parent = 0, $class = "brak") {
    //truncated
    }
}

このアクション ヘルパーを呼び出すと、次のようになります。

 $helper = $this->_helper->gallery($request);

ビューにも修正が必要です。

<!-- remember to 'echo' everything in the view -->
<?php echo $this->gallery;?>

古いコードが機能し、ZF が機能しない正確な理由については、私には言えません。ただし、問題の多くは、古いコードの Mysql 関数と、Zend Framework のMysqliまたはPDOデータベースアダプターの違いにあると思われます。

于 2012-12-31T02:07:13.287 に答える