2012-05-16 34 views
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(); 
} 

在这段代码中我获取所有部分和undersection数据(ID &名)。

数据库模型Section.php

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

数据库模型Undersection.php

<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> 

目前它:从有关数据输出的主视图 “index.phtml”

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

片段显示每个部分下的所有低点。

+1

什么是你的数据库结构?您目前没有任何将部分与底标联系在一起的部分 –

+0

部分表具有主键“section_id”,它是底部表中的外键。 – ositra

回答

1

您的代码当前显示每个部分下的所有下标的原因是因为您使用嵌套的for循环,其中内部循环始终是相同的。即你总是迭代相同的底下集合。您需要为部分和底部定义一种方法来建立父子关系。

这里是我会大致结构是:

DB结构(表名:部分):

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(),而这 - $> undersections

最大的好处变化你从这里得到的是你的模型现在是完全递归的。你的底下可以有自己的孩子部分,等等。

+0

谢谢你的回答,Jens!我会试试看,并留下我的反馈。 我有一个关于你提出的数据库结构的问题。它不应该也有一个sectionID字段? 'ID INT NOT NULL AUTO_INCREMENT parentId的INT DEFAULT 0 sectionId INT SECTION_NAME TINYTEXT' – ositra

+0

我真的不明白什么的getChildren()函数会做。请问你能否解释一下? – ositra

+0

getChildren当然应该在控制器中完成,而不是在视图中完成。 –

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(); 
相关问题