views:

155

answers:

4

I am programming my first real PHP website and am wondering how to make my code more readable to myself. The reference book I am using is PHP and MySQL Web Development 4th ed.

The aforementioned book gives three approaches to separating logic and content:

  • include files
  • function or class API
  • template system

I haven't chosen any of these yet, as wrapping my brains around these concepts is taking some time. However, my code has become some hybrid of the first two as I am just copy-pasting away here and modifying as I go.

On presentation side, all of my pages have these common elements: header, top navigation, sidebar navigation, content, right sidebar and footer.

The function-based examples in the book suggest that I could have these display functions that handle all the presentation example. So, my page code will be like this:

display_header();
display_navigation();
display_content();
display_footer();

However, I don't like this because the examples in the book have these print statements with HTML and PHP mixed up like this:

echo "<tr bgcolor=\"".$color."\"><td><a href=\"".$url."\">" ...

I would rather like to have HTML with some PHP in the middle, not the other way round.

I am thinking of making my pages so that at the beginning of my page, I will fetch all the data from database and put it in arrays. I will also get the data for variables. If there are any errors in any of these processes, I will put them into error strings.

Then, at the HTML code, I will loop through these arrays using foreach and display the content. In some cases, there will be some variables that will be shown. If there is an error variable that is set, I will display that at the proper position.

(As a side note: The thing I do not understand is that in most example code, if some database query or whatnot gives an error, there is always:

else echo 'Error';

This baffles me, because when the example code gives an error, it is sometimes echoed out even before the HTML has started...)

For people who have used ASP.NET, I have gotten somewhat used to the code-behind files and lblError and I am trying to do something similar here.

The thing I haven't figured out is how could I do this "do logic first, then presentation" thing so that I would not have to replicate for example the navigation logic and navigation presentation in all of the pages.

Should I do some include files or could I use functions here but a little bit differently? Are there any good articles where these "styles" of separating presentation and logic are explained a little bit more thoroughly. The book I have only has one paragraph about this stuff.

What I am thinking is that I am talking about some concepts or ways of doing PHP programming here, but I just don't know the terms for them yet.

I know this isn't a straight forward question, I just need some help in organizing my thoughts.

+1  A: 

sound like a template engine is what you are looking for - ask google for a lot of results. personally, i like smarty very much.

(and throw away that book, sound like it's... old)

oezi
Oh yeah. Now that I look at my question and look at Smarty, it seems to do things very much the same way as I was thinking about. However, I am wondering if I need smarty or if I could just do things in a "smarty" way.
Markus Ossi
+1  A: 

Never echo out HTML with PHP. Instead write it inline (without evil short tags) as

<tr class="<?php echo $myclass; ?>">

Other options for helping to separate out the logic / view would be to use a PHP Framework like CodeIgniter.

I would ditch the book and instead focus more on learning core PHP skills like functions, classes, etc. Then start playing the the several popular frameworks out there.

As a side note: The thing I do not understand is that in most example code, if some database query or whatnot gives an error, there is always:

That's because they are displaying the errors incorrectly. You should either store the errors in a sesssion and then display them on the page (clearing them as well) or throw them into the error log with the error_log function. error_log("Something happened in MyClass");

The thing I haven't figured out is how could I do this "do logic first, then presentation" thing so that I would not have to replicate for example the navigation logic and navigation presentation in all of the pages.

Think of things in a MVC approach. You call the controller (logic) first. It figures out what is needed. If it needs data from the database it invokes a Model and requests it. Then it formats it, adds other data, runs additional queries, and then passes it to the view.

Josh K
Thanks a lot for the input on errors and echo. I have been looking at CodeIgniter pages and I like the idea of MVC, but it has been a bit too much for me to understand just yet. That is why I am hoping to find way to do things wisely with just PHP.
Markus Ossi
@Markus: Then use includes at the top of pages. If a page has logic (form input, database work, etc) then try to stick all the logic one page, and use the `$_SESSION` along with redirects to provide a visual flow.
Josh K
+1  A: 

MVC (Model View Controller) sounds like it might suit your needs. You can read about that here.

Mike
+1  A: 

This might help in the broader sense: Organizing your PHP projects.

Babiker
Nice, thank you very much for this.
Markus Ossi