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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:50:06+00:00 2026-05-20T17:50:06+00:00

So I am still somewhat new to PHP, MySQL, and Javascript but I have

  • 0

So I am still somewhat new to PHP, MySQL, and Javascript but I have been working on a project so I have been learning fast. However, I feel like in my php pages I am mixing too much HTML and PHP. At first I thought it was standard practice but someone on SO informed me of how you should not mix the two and I began looking at my code.

For example, when loading a page of submissions I have a loop that looks something like this (condensed version):

<?php
while ($row = mysql_fetch_assoc($submissionQuery))
{
  $submissionID = $row['id'];
?>
  <div class="submission" id="submission<?php echo $submissionID; ?>">
    <h3><?php echo $row['title']; ?></h3>
  </div>
<?php } ?>

The reason why I don’t just enclose the entire block in PHP is because there is not only an h3 there and I don’t want to use mass-echo statements.

In my opinion this looks terrible and I’d like to know a better way of doing it. I suppose that I could store all the submissions in to an array and then loop through them later on but I see several downsides to this:

1) Unnecessarily storing values in to an array. Only going to recall them immediately after.

2) If there are a lot of submissions there may not be enough memory to store them.

3) Would require more code.

4) Would still have to loop through the array later on and in that case I am still mixing PHP and HTML (just to a lesser degree)

I don’t know. I just need some advice as to best handle this because I don’t want to do things the wrong way and then have to refactor everything later on when something breaks or gets too complex.

  • 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-20T17:50:07+00:00Added an answer on May 20, 2026 at 5:50 pm

    When working with PHP, it’s almost impossible to not mix PHP and HTML. But this doesn’t mean you’re mixing your presentation logic with your business logic. Those are two very distinct things.

    In your example you’re mixing your code. A better way to do it would be to.

    You have a file, call it submission_proccess.php, in that file you’d have this code.

    $submission = array();
    while ($row = mysql_fetch_assoc($submissionQuery)) {
       $submission[] = $row;
    }
    
    //more logic
    //include your view submission_view.php
    

    Now submission_view.php is your view, so to speak. There you would have something like this

      <?php foreach ($submissions as $submission) : ?>
      <div class="submission" id="submission<?php echo $submission['id']; ?>">
        <h3><?php echo $submission['title']; ?></h3>
      </div>
      <?php endforeach; ?>
    

    The important thing here is to see that you won’t have to touch your view file, if say you want to modify your query or maybe filter it. Your business logic is kept separate from how you show it. You can even remove the HTML include and just have a file that takes the $submission variable and outputs it as JSON.

    Some things you should be looking at is

    • PHP frameworks – Zend, CakePHP, CodeIgnitor, Symphony, Kohona and many more. They help you to separate these parts
    • Another thing is PHP alternate syntax for control structures
    • Also, PHP templating systems like Smarty. Some might argue that using Smarty just adds another unnecessary layer, but it’s very useful when you don’t want to go for a full blown framework stack.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I still very new using Subversion. Is it possible to have a working copy
Where I work we're still on .Net 2.0, but I'm somewhat familiar with the
still new to the world of linq, and i need some help flatening a
Still new to Objective C, and I'm having some trouble that I just can't
I still have a large number of floppies. On some of them there probably
I still feel C++ offers some things that can't be beaten. It's not my
Still 'diving in' to Python, and want to make sure I'm not overlooking something.
Still struggling to understand what best practices are with respect to macros. I'm attempting
still trying to find where i would use the yield keyword in a real
Okay still fighting with doing some SqlCacheDependecy in my Asp.net MVC application I got

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.