Separation of presentation and business logic in PHP -


I am programming my first real PHP website and wondering how to make my code more readable for me .

The above book introduces three ways to isolate logic and content:

  • Include files
  • function or class API
  • Template system

I have not chosen any of these yet, because it takes some time to wrap my brain around these concepts, though, some of my code Hybrid is made because I just copy and paste it and change it as I Go.

On the presentation side, all of my pages have these general elements: Header, Top Navigation, Sidebar Navigation, Content, Right Sidebar and Footer.

A work-based example in the book illustrates that I can have these display functions that handle all the presentation instances. Therefore, my page code will look like this:

  display_header (); Display_navigation (); Display_content (); Display_footer ();  

However, I do not like this because in the examples given in the book, these print statements meet with HTML and PHMM:

  echo " & Lt; tr bgcolor = \ "". $ Color. "\" & Gt; & lt; td & gt; & lt; a href = \ "". $ Url. "\" & Gt; "... < / Code> 

I want to HTML with some PHP in the middle, not the other way.

I am thinking of making my pages so that at the beginning of my page, I will get all the data from the database and put it in the array. I will get the data for the variable. If any of these processes are errors, then I will put them in an error string.

Then, on the HTML code, I will display content using forehocks through these arrays. In some cases, some variables that will be shown. If an error variable is set, then I will display it at the proper position.

(As a side note: This thing which I do not understand is that in most example code, if some database query Whatnot returns an error, there always is:

< Pre> else echo 'error';

This makes me baffle, because when the example code returns an error, sometimes the buzz resumes before HTML is started ...)

For people who use ASP.NET, I have some code-behind files and I have been used in LLLRR and I am trying to do something similar here. I do not know that this is the thing that I can do this. Make the argument first, then do the presentation "do the thing So that I have to repeat the examples of navigation logic and navigation presentation in all. Pages

Should I include some files or can I use the function here, but in a slightly different way? Is there a good article, where the "styles" of separation of presentation and logic are explained a bit more well? I have only one paragraph about this stuff.

What I am thinking is that I am talking about some concepts or methods of PHP programming but I do not have the conditions for them so far.

I know that this is not a straight question, I need some help in organizing my thoughts.

never buys HTC with PHP instead it is inline ( Without a bad tag )

& lt; Tr class = "& lt ;? php echo $ myclass ;? gt;" & Gt;

Other options to help to isolate the logic / view, the PHP framework must be used.

I dug the book and instead the main PHP skills like tasks, sections, etc. Then start playing out there.

As a note on the one hand: I do not understand that in most example code, if a database queries or gives an error, this always happens:

This is because they are displaying errors incorrectly, you should either store errors in a Cassius and then display them on the page (also clear them) or they In code A. error_log function. error_log ("Something happened in MyClass");

I do not know how to do this "how to do logic first, then the presentation" so that I have to repeat the examples of navigation logic and navigation presentation in all the pages.

Think about things in an approach that you call the controller first, it explains what is necessary. If it requires data from the database then it invites a model and requests it. Then it formats it, adds other data, runs additional questions, and then sends it in view


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -