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 8044857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:21:47+00:00 2026-06-05T05:21:47+00:00

I am new to Zend Framework and would like advice on how best to

  • 0

I am new to Zend Framework and would like advice on how best to loop through database data to generate a list of links. My understanding is that the Model should contain most of the application logic with the controllers and views being as light as possible.

I am querying the db to get a set of records and I want to loop through them and generate HTML links. Psuedocode below.

CONTROLLER:

$this->view->myList = MODEL->generateHtml();

MODEL:

function generateHtml() {
    query db

    loop through record set

    build a string of html within loop including links

    return string to controller

}

VIEW:

echo $this->myList;

This seems to put the logic in the model and leave the controller light and the view only for rendering.

One problem I have is that I want to use $this->view->url to generate routing links in the html I output but cannot do that in the model. The reading I have done online suggests that you should not be building html in the model. I can generate an array of the required data in the model and return that and then loop through that in either the controller or the view to generate the html but am unsure as to what is the correct approach and would appreciate some advice.

Thanks for any help given.


NEW PROBLEM – UPDATED CODE:

Hi again….have tired suggestion below but have a different issue now.

My code is now:

MODEL:

not used for this test. will return array similar to array created in controller.

CONTROLLER:

    $aStoryList = array( 
        array(
           'headline' => 'Headline 1', 
           'story' => 'Story 1' 
        ), 
        array(
           'headline' => 'Headline 2',
           'story' => 'Story 2'
        )
    );
    $this->view->aStoryList = $aStoryList;

VIEW:

echo $this->partialLoop('partials/storyList.phtml', $this->aStoryList);

storyList.phtml:

echo "<br />" . $this->headline . $this->story;

I have placed the partial thus….

views/partials/storyList.phtml

This placement and the path used in the view are derived from the answer to this stackoverflow question – Where do I save partial (views) in Zend Framework, to be accessible for all Views in my App?

When I run this I get the following error

Message: script ‘partials/storyList.phtml’ not found in path (/home/sites/xxxxx.com/public_html/xxxxxxx/application/views/scripts/)

Pulling my hair out now!

  • 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-05T05:21:48+00:00Added an answer on June 5, 2026 at 5:21 am

    The model should be used to pull data from your data source, but should not be generating any HTML markup. Save the HTML generation for the view. The controller will be the glue between the model and view; that is the controller will do the work to fetch the data and hand it off to the view where the output is generated.

    In your particular case, the PartialLoop View Helper should be useful for creating markup in a loop.

    I’d propose the following Pseudocode instead of what you had posted above:

    CONTROLLER:

    $this->view->myList = MODEL->getListOfItems();  // return an array of data
    

    MODEL:

    function getListOfItems() {
        $results = array(); // array of data to return
    
        // query db
    
        // loop over result set
        foreach($result as $row) {
            $results[] = $row;
        }
    
        return $results;
    }
    

    VIEW:

    <?php echo $this->partialLoop('myList.phtml', $this->myList);
    

    myList.phtml View Partial:

    <tr>
        <td><a href="<?php echo $this->url(array('id' => $this->id))"><?php echo $this->username ?></a></td>
        <td><?php echo $this->firstName ?> <?php echo $this->lastName ?></td>
        <td><?php echo $this->email ?></td>
    </tr>
    

    To summarize:

    • Controller queries model for data
    • Model returns an array of results
    • Controller passes array directly to view
    • View calls partialLoop helper and passes the array from the model
    • partialLoop helper iterates over all of the results, passing them one at a time to myList.phtml (note how the variable scope becomes local to the view partial).

    My example assumes the array returned by the model contains the keys id, username, firstName, lastName.

    Hope that helps, feel free to comment if you have any questions.

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

Sidebar

Related Questions

I'm new to Zend Framework. I would like to know how to implement zend
I am new to zend framework I would like to know about bootstrap.php and
Im new to Zend framework, i would like to know how to add a
I am quite new in Zend Framework and RSS too. I would like to
We are using zend framework 2 for a new application, i would like to
I would like to use the Mail module of Zend Framework. I have extracted
I am testing my Zend Framework application and would like to test that something
I'm just new to Zend Framework. Currently what I'm trying to do is when
I am new to Zend framework. I am having a controller interact with a
I am pretty new to the zend framework and the JQuery helper. I have

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.