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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:07:41+00:00 2026-05-22T18:07:41+00:00

what could be better for respect the MVC? make a view with a for

  • 0

what could be better for respect the MVC?

make a view with a for loop like this:

<?php foreach($posts as $post){
            echo '<div class="post">'.$post.'</div>';
      }
?>

where $posts hold all the body of the post.

or in the controler make something like this:

<?php foreach($posts as $post){
          $html = $html + '<div class="post">'.$post.'</div>';
      }
?>

then pass the $html to the view and display them.

  • 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-05-22T18:07:41+00:00Added an answer on May 22, 2026 at 6:07 pm

    Simple logic such as loops are acceptable in views, you don’t want any business logic in there. Since the logic of this loop is for display purposes, I would use the first.

    Generally in template files, the foreach: endforeach syntax is used.

    <?php foreach($posts as $post):?>
         <div class="post"><?php echo $post;?></div>
    <?php endforeach;?> 
    

    Or if short tags are enabled (Please note that this is often disabled on production servers, but <?php cannot be disabled, so I’d recommend the above method):

    <? foreach($posts as $post):?>
         <div class="post"><?=$post?></div>
    <? endforeach;?> 
    

    However this is a convention as opposed to a rule.

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

Sidebar

Related Questions

The [sample] code below seems like it could be better in a loop or
(This question could be better off on math, but im not sure) https://i.stack.imgur.com/ujPJG.png This
I feel like the following code is suboptimal, and that it could be better.
Could anyone explain with some examples when it is better to call functions by
I'm sorry I could not think of a better title. The problem is the
Could anyone recommend me a good open source .Net based wiki project? Better with
Is there a better way in WCF werein we could avoid the translation betwee
I have an MVC application with a single model and several views (something like
I could not find a better title for my question, but you are welcome
An algorithm of mine could be better readable if I could use a postcondition

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.