Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to code beautiful PHP together with HTML? [closed]

Tags:

html

php

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

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

like image 771
linjuming Avatar asked Nov 13 '12 03:11

linjuming


People also ask

How do I combine HTML and PHP codes in one page?

Mixing HTML and PHP. PHP code is normally mixed with HTML tags. PHP is an embedded language, meaning that you can jump between raw HTML code and PHP without sacrificing readability. In order to embed PHP code with HTML, the PHP must be set apart using PHP start and end tags.

How does HTML interact with PHP?

PHP and HTML interact a lot: PHP can generate HTML, and HTML can pass information to PHP. Before reading these faqs, it's important you learn how to retrieve variables from external sources. The manual page on this topic includes many examples as well.

Can I write HTML code in PHP?

While HTML and PHP are two separate programming languages, you might want to use both of them on the same page to take advantage of what they both offer. With one or both of these methods, you can easily embed HTML code in your PHP pages to format them better and make them more user-friendly.


1 Answers

There are methodologies out there that help with this issue, such as Model-View-Controller (MVC). MVC seperates your data layer, from your logic, from your presentation (UI) layers. A framework such as CodeIgniter would help you in this case. Even if you weren't going to use an existing framework, seperating out your data models from your business logic and then from your UI 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 information from your other PHP files (the "Controllers"), so that instead of this:

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

you can do this:

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

In this scenario, all logic and data access are handled before you attempt to display the page.

like image 66
ajacian81 Avatar answered Oct 25 '22 22:10

ajacian81