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

選択する列を設定する必要があります。

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 に答える
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>

$this->undersections の代わりに $sections->getChildren() を使用する変更に注意してください

これにより得られる最大の利点は、モデルが完全に再帰的になったことです。アンダーセクションは、独自の子セクションを持つことができます。

于 2012-05-16T13:27:16.060 に答える