I feel that I am mixing my presentation logic too much with my model logic. Some help?

So, I'm still a little new to PHP, MySQL and Javascript, but I was working on a project, so I'm learning fast. However, I feel like I am mixing too much HTML and PHP on my php pages. At first I thought it was standard practice, but someone from SO told me how you should not mix these two, and I started looking at my code.

For example, when loading the views page, I have a loop that looks something like this (compressed 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 I’m not just embedding the whole block in PHP is because not only h3 is there, but I don’t want to use mass echo expressions.

In my opinion, it looks awful, and I would like to know the best way to do this. I believe that I could store all the materials in an array and then scroll through them later, but I see a few drawbacks:

1) Without the need to store values ​​in an array. Just going to remember them right after.

2) If there are many messages, there may not be enough memory to store them.

3) More code required.

4) Still have to iterate over the array later, in which case I'm still mixing PHP and HTML (only to a lesser extent)

I dont know. I just need some advice to better deal with this, because I don’t want to do something wrong, and then I will have to reorganize everything later when something breaks or becomes too complicated.

+4
source share
3 answers

When working with PHP, it's almost impossible not to mix PHP and HTML. But this does not mean that you are mixing your presentation logic with your business logic. These are two very different things.

In your example, you are mixing your code. The best way to do this would be.

You have a file, name it submit_proccess.php, in this file you will 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 will 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; ?> 

It is important to note that you do not need to touch the view file if you want to modify your query or possibly filter it. Your business logic is kept separate from how you display it. You can even remove the HTML and just have a file that accepts the $submission variable and displays it as JSON.

Some things you should look at are

  • PHP frameworks - Zend, CakePHP, CodeIgnitor, Symphony, Kohona and many others. They help you separate these parts.
  • One More Thing Alternative PHP Syntax for Control Structures
  • In addition, PHP templates such as Smarty. Some may argue that using Smarty just adds another unnecessary level, but it is very useful when you do not want to go on a full-fledged stack of frames.
+4
source

I really don't like ANY server-side code in my view templates. I prefer to have only place owners. However, most frameworks contain loops and echo variables in the view.

Look at a template system like smarty (I hate smarty BTW), but they will at least give you the opportunity to separate things like that, which is always good.

0
source

Separation of logic at another level of abstraction does not exclude and does not prevent mixing PHP and HTML or other codes. The point is to achieve an appropriate separation of problems and the right degree of mixing.

In the presentation presentation layer, you will encode HTML, CSS, some Javascript, and some (possibly very small) PHP. This is how PHP works. But you can keep this code simple, just glue for the details.

At the level of business logic, you will write in PHP, perhaps in some SQL or some kind of javascript.

To facilitate separation of problems, I usually separate layers in different source files. During build, build scripts correctly mix the code with the actual final artifacts and renders.

Finally, you don’t have to invent everything all the time. Take a look at some of the application frameworks and templates for PHP. Some may have direct help; or at least you get some ideas and development strategies that will help you.

0
source

Source: https://habr.com/ru/post/1344633/


All Articles