How to implement MVC style for my PHP / SQL / HTML / CSS code?

I am developing a program to visualize some data. My program takes specific data from a MySQL database and draws some graphs (libchart library), creates some tables, etc.

My problem is that right now his hellish code is there. I have about 7 php files (index, graphic page, gallery, etc.) Together with HTML / CSS and PHP / SQL code (some of them just have the php extension, but only have HTML inside). I have no problem reading and understanding the project at the moment, but I think if someone else tried, he could get a headache. In addition, continuing programming like this is impractical because in the future a project can be easily scalable.

Do you have any suggestions on how to successfully separate HTML / CSS from PHP / SQL? I do not want to use the framework, since I am not doing anything that requires user input, session processing, etc. I just run a few queries and visualize the results. I mainly talk about architecture here, and, if applicable, perhaps a script to help me (I read about Smarty, but I'm not sure what I need).

+8
html css sql php model-view-controller
source share
2 answers

Do you have any suggestion on how to successfully separate HTML / CSS from PHP / SQL?

Congratulations on how you can improve the code. This is a prerequisite, you should want to improve it, and the topic is lengthy. Therefore, your will is crucial.

I start lightly and then try to give some advice. Since you are lacking in experience, find one point to start with, and most importantly, the last of the list below. But first, first:

To separate something from each other, you need to have code that separates:

[HTML/CSS/PHP/SQL] [HTML/CSS] <--> [SEPARATOR] <--> [PHP/SQL] 

The separator here is also PHP code, but I think you get the idea.

As you can see, only the Separator speaks HTML / CSS and PHP / SQL.

Thus, for this to work, HTML / CSS and PHP / SQL must have an interface with Separator (the line between them). A.

Usually in a program you transfer data that is processed. The data is quite dynamic and can have complex complexity, especially if you pass the data to the output procedure, which should format it correctly.

There are several ways to use such a separator (or several of them). You can add up your software or provide components that perform actions in your area or field. For example. you have a database level or database component that takes care of interacting with the database.

Or you have a template engine that will put your strings and arrays in some readable HTML.

In short, this is a pasta theory of software design:

  • Spaghetti code - all in one, the code is very intertwined, preferably with Bolognese or Aglio, Olio and Peperonchino.
  • Caressed code . Layered, on one layer there are two other layers with which it interacts (except for the lower or upper), always with Béchamel sauce.
  • Tortelini Code Small ingredients that just do their job, they have meat or spicy vegetables inside.

As we eat different pasta in our lives, when programming, we need to deal with all of these different types of code, and we develop our own preferred taste over time. In childhood, we feed, but over time, we begin to cook something of our own and vary recipes.

So, I believe that you just don’t need to have an MVC Framework X right now with a huge amount of awesome over the next weeks just because someone told you what it is. And before the meal there is a tasting, right? Not to mention fast foods, you know how these noodles with sauce are in the package - just add water. Urgh.

I don’t know what data you need and what the input is. Below are some rough refactoring tips for applications that display HTML / CSS and interact with a MySQL database. This cannot be a complete list, and descriptions can only roughly outline some thoughts:

  • Move CSS from HTML. Make good use of the selector in the associated CSS definition and replace any style attributes if you still have some. This makes your CSS code reusable and more modular. This will help you find flaws inside your HTML and separate the structure (HTML) from the presentation (CSS). Effective HTML starts with the efficient use of CSS, the two are very powerful together, and often this will make your programmatic output routines easier.
  • Move business logic from HTML. Both HTML and your code can be beasts, so it's best to separate them. They have a tendency to fight each other, and since both of them are very strong, the struggle will continue until you develop your application that distracts you from the work that you need to do.
    Think about whether you already need to have complex output inside your application, or if you can just pass arrays with subelements (the key is var, var can contain a string or a number or another var array). This is usually necessary to transfer even complex data to a view or template. You HTML then only needs to drop some elements of the array and / t 22> over the subarrays. This is a very easy way to create a template. You can use PHP to do this, so you are really very flexible (just draw a border whose code belongs to your view layer and which is part of the application, for example, provides values ​​for the view).
  • Move SQL from your code. Move the database interaction code. Create yourself one or more objects that have methods that return data as you need (consume) in your actual processing code, for example $component->getThatData() , which then returns the data in a normalized form. Then these components use a special database component to exchange information with the database. In the application code (business logic), use only the database component and, preferably, the objects you create to receive the data, so you no longer have the SQL string inside the main code.
  • Divide and encode the application code: divide the code into Transaction Scripts . They are often easily created from existing spaghetti code and are likely to become mentioned by the Separator that you are looking for in the medium term. Then they will have a role to process the data and transfer it (to the output / presentation).
  • Use clean language. If you have complex formatted string data that isn’t normalized, write the Parser classes themselves that do your work and that can be easily reused (if this applies to your application). Since you should look forward to minimizing the use of plain SQL in your code, you should also look forward to porting complex regular expressions. Encapsulating what is changing is key. The same applies to lengthy routines, just to process some data (for example, sort, organize and change it in a different format), move them to the components of each of them, and think about how to make them available and reused.
  • Create your code: learn about the logic of how you call functionality in your program. You can try to separate the functionality from the way it is called. For example. some procedure that invokes any of the transaction scenarios. This may not be necessary if you request the PHP files directly through the browser, as these are your transaction scripts, and the web server will take care to allow the command to be sent at the URL to your application for the transaction script. But then you have to wrap any logic needed to process the incoming command and its parameters in reusable components (for example, a request class that contains standard code to get the URL and variables from the HTTP request).
  • Create a common entry point by including the same file at the very top of all files that are called through the browser. Then you can place the general code (for example, configure the application session state object and initialize the database component), as well as the Application Controller
  • Remove duplication by looking for literally duplicated code. Include it in a function or class. Create a library folder for your own application in which you have included your applications. If you follow the general scheme with Classnames and Namespacing, you can easily use the autoloader to simplify inclusion. Make your library separate from third-party code. Put all the third-party code in your own library folder with one subdirectory for each third-party component.
  • Use lightweight, existing components. Light weight is important because you already have your own code, you don’t want to turn around and immediately click on the framework. Existing is important because you do not want to reinvent the wheel. You will have enough work for your own code refactoring. Once you feel better in your application, and you still have the strength and will, you can always write everything new. But if you are single or in a small team, Existing is quite powerful. Simple libraries are, for example:
  • Create application state, for example. as an object that you can use if some components need to know about the state of the application or each other without direct interaction. By default in PHP, if you don't have one, global and global static variables are used to create state. However, these variables can make your life tough as your code grows. When you create an application state object, it cleans up which components use it, access to it can be controlled (for example, calling a method instead of reading a variable that can help with debugging) and the components can also figure out the correct time in the application flow for entry into action . It is also a good tool to refactor your code over time.
  • Save the working application, keep your code in a state to run. Ideally, this would be supported by automatic tests. Think that you need to rewrite a lot. For example, if you start integrating a database component, do it. Move all your existing code one step. So who tells you that it still works? Use git for better cancellation and testing. This is more important than choosing the right library. Saving a working application is also always a key point, because why are you changing it, right?
+19
source share

Why not use a template engine? TWIG is very easy to use and great for this kind of thing. It is often used with the Symfony framework, but it can be easily used on its own.

+2
source share

All Articles