0

我正在使用 Zend Framework 和 phpMyAdmin (MySQL) 在 PHP 中创建一个留言板。我是 Zend Framework 的初学者,在 PHP 方面做得不多,所以请在你的答案中尽可能简单。

我已经在数据库中保存了留言板的结构(部分、下部分和下部分)。我需要显示论坛的部分及其相应的underSections。问题是 - 我如何在其下显示特定部分的下部分?

IndexController.php

 public function indexAction()
{
    $sections = new Model_Sections();
    $this->view->sections = $sections->fetchAll();

    $undersections = new Model_Undersections();
    $this->view->undersections = $undersections->fetchAll();
}

在此代码中,我获取所有部分和部分数据(id 和名称)。

数据库模型Section.php

class Model_Sections extends Zend_Db_Table_Abstract
{
    protected $_name = 'sections';
}

数据库模型Undersection.php

class Model_Undersections extends Zend_Db_Table_Abstract
{
    protected $_name = 'undersections';
}

主视图“index.phtml”中有关数据输出的片段:

<div class="section">
   <?php foreach($this->sections as $sections) : ?>

   <!-- Generates names of sections -->
   <h1><?php echo $this->escape($sections->section_name);?></h1>

      <!-- Generates names of undersections -->
      <?php foreach($this->undersections as $undersections) : ?>
         <div class="underSection">
            <h2>
              <a href=" ***link to some controller according to the undersection*** ">
                 <?php echo $this->escape($undersections->undersection_name);?>
              </a>
            </h2>
         </div>
      <?php endforeach; ?>
   <?php endforeach; ?>
 </div>

目前它显示每个部分下的所有下部分。

4

2 回答 2

1

您的代码当前显示每个部分下的所有下部分的原因是因为您使用了嵌套的 for 循环,其中内部循环始终相同。即,您总是在相同的下节集合上进行迭代。您需要为sections 和undersections 定义一种父子关系的方式。

以下是我将如何大致构建它:

DB结构(表名:sections):

id INT NOT NULL AUTO_INCREMENT
parentId INT DEFAULT 0
section_name TINYTEXT

因此,所有部分的数据都将存在于同一个数据库表中。当您插入一个顶级部分时,您只需将 parentId 列保留为 0。当您插入一个下部分时,您将插入父部分的 id 值。

我还会更改您的模型,以便您没有 Model_Section 和 Model_Undersection。相反,在 Model_Section 类中有一个名为 getChildren() 的函数,它将向您返回属于该特定 Model_Section 实例的所有部分的集合。

控制器动作:

public function indexAction()
{
    $sections = new Model_Sections();
    $this->view->sections = $sections->fetchAll();
}

查看脚本:

<div class="section">
   <?php foreach($this->sections as $sections) : ?>

   <!-- Generates names of sections -->
   <h1><?php echo $this->escape($sections->section_name);?></h1>

      <!-- Generates names of undersections -->
      <?php foreach($sections->getChildren() as $undersections) : ?>
         <div class="underSection">
            <h2>
              <a href=" ***link to some controller according to the undersection*** ">
                 <?php echo $this->escape($undersections->section_name);?>
              </a>
            </h2>
         </div>
      <?php endforeach; ?>
   <?php endforeach; ?>
 </div>

注意使用 $sections->getChildren() 而不是 $this->undersections 的变化

您从中获得的最大好处是您的模型现在是完全递归的。您的下部分可以有自己的子部分,依此类推。

于 2012-05-16T13:27:16.060 回答
1

您应该设置要选择的列:

class Model_Sections extends Zend_Db_Table_Abstract
{
    protected $_name = 'sections';
    public function getSections()
    {
        $select = $this->select()
            ->from($this->_name, array('col1', 'col2')); // set your columns
        return $this->fetchAll($select);
    }
}

在控制器中:

$sections = new Model_Sections();
$this->view->sections = $sections->getSections();
于 2012-05-17T08:36:38.973 回答