Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7975527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:39:58+00:00 2026-06-04T08:39:58+00:00

I am creating a message board in PHP using Zend Framework and phpMyAdmin (MySQL).

  • 0

I am creating a message board in PHP using Zend Framework and phpMyAdmin (MySQL). I’m a beginner with Zend Framework and haven’t done much in PHP, so please be as simplistic as possible in your answers.

I’ve saved the structure of the message board (sections, under-sections and under-under-sections) in the database. I need to display the sections of the forum and their corresponding underSections. The question is – how do I display the under-sections of a particular section under it?

IndexController.php:

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

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

In this code I fetch all the section and undersection data (id & name).

Database model Section.php:

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

Database model Undersection.php:

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

Fragment from the main view “index.phtml” concerning output of data:

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

Currently it displays all undersections under every section.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T08:40:00+00:00Added an answer on June 4, 2026 at 8:40 am

    The reason your code currently is displaying all undersections under each section is because you use a nested for-loop in which the inner loop is always the same. I.e. you are always iterating over the same collection of undersections. You need to define a way for the sections and undersections to have a parent-child relationship.

    Here is how I would roughly structure it:

    DB structure (table name: sections):

    id INT NOT NULL AUTO_INCREMENT
    parentId INT DEFAULT 0
    section_name TINYTEXT
    

    So the data for all sections would live in the same database table. When you insert a top level section, you would simply leave the parentId column = 0. When you insert an undersection, you would insert the id value of the parent section.

    I would also change your model so that you don’t have a Model_Section and an Model_Undersection. Instead, have a function within the Model_Section class called e.g. getChildren() which would return to you a collection of all sections that belong to that particular Model_Section instance.

    Controller action:

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

    View script:

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

    Note the change in using $sections->getChildren() instead of $this->undersections

    The biggest benefit you get from this is that your model now is completely recursive. Your undersections can have child sections of their own, and so on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im creating a simple message board with php/mysql. Users enter their name and a
I am creating a small message board in PHP and I need to simplify
I am creating private message system in php/mysql. I created message tables: messages(message_id|sender_id|receiver_id|message_text|created_time) Here
How would I go about creating a tripcode system (secure) for a message board
When creating a new message using Mail on the iPhone, and after typing the
I am creating a new event source and logging a message using the code
I am creating email message using Apache James. I add TextBody to message with
I'm creating a threaded message board and I'm trying to keep it simple. There's
I am creating a small message board and I am stuck I can select
I'm creating an email message using CDO object (and VB6, but that doesn't really

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.