How to code beautiful PHP with HTML?

When coding PHP along with HTML, it always seems confusing and very painful. The structure is not easy to understand.

Can anyone talk about how to code PHP with HTML in a good structure? php code

+6
source share
3 answers

There are methodologies that help in solving this problem, such as Model-View-Controller (MVC). MVC separates your data layer from your logic from presentation layers (UI). A framework such as CodeIgniter will help you in this case. Even if you are not going to use the existing infrastructure, separating your data models from your business logic and then from the user interface objects is relatively easy.

Edit: let's say you have a shopping cart page. You can have a “view” file called cart.php, and then you can send it from other PHP files (“Controllers”), so instead:

<div id = 'special_price'> <?php $result = mysql_query("SELECT price FROM pricelists"); $row = mysql_fetch_assoc($result); echo $row["price"]; ?> </div> 

You can do it:

 <div id = 'special_price'><?= $price ?></div> 

In this case, all the logic and access to the data is processed before you try to display the page.

+6
source

@ ajacian81 is right, but there are additional options. He mentioned how to write all your code in accordance with the MVC framework. MVC architecture is a great way to share the challenges of complex web applications. MVC divides application logic, database logic, and views into 3 separate places. However, reorganizing the php + html mixed codebase into an MVC framework can take a long time. If you want to remove php from your html without completely reorganizing your code base, consider using a template engine such as Twig or Smarty .

If you use the template engine, you can separate your php and sql in one place and your html templates in another place. If you are using the MVC framework, you should split your php, sql and html into 3 different locations. If your priorities are short-lived, just use the template engine. If your priorities are long term, consider how to transform your code into an MVC framework.

+1
source

Php is so advanced that you really won’t get anything using the Persuasion Framework for custom builds. If you comment on your scripts and each beginning and end of each html and php section, you will be amazed at how quickly and how many fewer files it takes to run a full social site or social business portal. I use only three main files: config.php, ini.php, which stores all my sessions and cookies, and db_connect.php. All my pageviews are processed by Ajax without a page refresh, and all my forms are cast into some of these submissions to use Ajax without a page refresh. I used Ajax quite often for my entire URL request, which ellimizes the need for bulky frameworks.

-2
source

All Articles