I feel like I am mixing my presentational logic with my model logic too much. Some help?
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 lik开发者_C百科e 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.
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.
I really don't like ANY server side code in my view templates - I prefer to just have place holders. Most frameworks do however include loops and echo variables in the view.
Have a look at a templating system such as smarty (I hate smarty BTW) but they will at the very least give you opportunity to separate this kind of stuff out which is always a good thing.
Separating logic in diferent abstraction layer does not preclude nor prevent mixing PHP and HTML or other codes. The point is to achieve the appropiate separation of concerns and the right degree of mixing.
In the presentation logic layer you will code in HTML, CSS, some Javascript and some (probably very little) PHP. That's the way PHP works. But, you may keep this code simple, just the glue for the parts.
In the business logic layer, you will code in PHP, maybe some SQL, or some javascript.
To ease the separation of concerns I usually separate the layers in different source files. At build time the build scripts mix the code appropiately to the actual final artifacts and renders.
Finally, you don't need to reinvent everything yourself. Take a look at some of the application frameworks and template engines for PHP. Some may be of direct help; or, at least, you'll get some ideas and design strategies to base yours.
精彩评论