You should separate HTML and PHP code into separate files to make it
easily maintainable. Ideally, someone who knows HTML without any
knowledge of PHP would be able to change the layout of the web page
without breaking anything.
There are a bunch of examples of how to do this, usually falling
under the "MVC Design Pattern" title. That's Model, View, Controller.
I use a simple substitution system to embed "tags" that represent
data into my HTML files.
The html file would look something like:
<table>
<tr><td>First Name</td>
<td>{:FirstName:}</td>
<tr><td>Last Name</td>
<td>{:LastName:}</td>
</table>
The php file would be something like:
$tpl = file_get_contents('htmlfile.htm');
//Assign Data to tags
$data['{:FirstName:}'] = 'Brent';
$data['{:LastName:}'] = 'Baisley';
//Get Tags to search on
$tags = array_keys($data);
//Populate html template with data
$content = str_replace($tags, $data, $tpl);
echo $tpl;
That's an extremely simplified templating system and an over
simplified example. But it shows how you can easily completely
separate html from php, presentation from logic. I find it far easier
to work on than any of your three examples. Most importantly, it
allows more talented interface designer design your interface, while
you focus on the php and logic.
On Nov 16, 2005, at 11:12 AM, afan@xxxxxxxx wrote:
Hi to all,
always wondered what's better way to mix html and php code. Here
are three "ways" of the same code. Which one you prefer? (And why,
of caurse :))
Solution 1:
######################################################################
######################
<?php
require 'includes/header.php';
$cat_parent = $_GET['cat_parent'];
$cat_id = $_GET['cat_id'];
echo '<table border="0" align="center" cellpadding="1"
cellspacing="0" style="border: 1px solid #EBEBEB; padding: 25px;">';
echo ' <tr>';
echo ' <td align="left" height="35" valign="top"
colspan="2"><b><u>'. $_SESSION['CATEGORIES']['name']
[$cat_parent] .' > '. $_SESSION['CATEGORIES']['name'][$cat_id] .'</
u></b></td>';
echo ' <td align="center" valign="bottom" colspan="2"><a
href="new_product.php?cat_id='.$cat_id.'&cat_parent='.
$cat_parent.'" style="font-size: 11px; color: gray;">[ Add New
Product ]</a></td>';
echo ' </tr>';
$query = my_query("
SELECT chp.products_prod_id, p.prod_id, p.prod_name,
p.prod_no, p.prod_status
FROM categories_has_products as chp, products as p
WHERE chp.categories_cat_id = '".$cat_id."' AND
chp.products_prod_id = p.prod_id
", 0);
while($result = mysql_fetch_array($query))
{
echo ' <tr>';
echo ' <td align="left" valign="top">» '. $result
['prod_name'] .'</td>';
echo ' <td align="center" valign="top">[ '. $result
['prod_no'] .' ]</td>';
echo ' <td align="center" valign="top">';
echo ' <img src="../images/icon_edit2.gif" alt="EDIT"
width="14" height="14" hspace="0" vspace="0" border="0">';
echo ' <a href="products.php?cat_id='.
$cat_id.'&cat_parent='.$cat_parent.'&action=delete&prod_id='.$result
['prod_id'].'" onclick="return confirm(\'Do you really want to
delete this product?\');"><img src="../images/icon_delete.gif"
alt="DELETE" width="14" height="14" hspace="5" vspace="0"
border="0"></a>';
switch($result['prod_status'])
{
case 'live':
echo '<a href="products.php?
new_status=hidden&prod_id='.$result['prod_id'].'&cat_id='.
$cat_id.'&cat_parent='.$cat_parent.'"><img src="../images/
status_live.gif" alt="LIVE" width="13" height="13" hspace="2"
vspace="0" border="0"></a>';
break;
case 'hidden':
echo '<a href="products.php?
new_status=live&prod_id='.$result['prod_id'].'&cat_id='.
$cat_id.'&cat_parent='.$cat_parent.'"><img src="../images/
status_hidden.gif" alt="HIDDEN" width="13" height="13" hspace="2"
vspace="0" border="0"></a>';
break;
case 'temp':
echo '<b>[T]</b>';
break;
}
echo ' </td>';
echo ' </tr>';
}
echo '</table>';
include 'includes/footer.php';
?>
Solution 2:
######################################################################
######################
<?php
require 'includes/header.php';
$cat_parent = $_GET['cat_parent'];
$cat_id = $_GET['cat_id'];
?>
<table border="0" align="center" cellpadding="1" cellspacing="0"
style="border: 1px solid #EBEBEB; padding: 25px;">
<tr>
<td align="left" height="35" valign="top"
colspan="2"><b><u><?= $_SESSION['CATEGORIES']['name'][$cat_parent] ?
> > <?= $_SESSION['CATEGORIES']['name'][$cat_id] ?></u></b></td>
<td align="center" valign="bottom" colspan="2"><a
href="new_product.php?cat_id=<?= $cat_id ?>&cat_parent=<?=
$cat_parent ?>" style="font-size: 11px; color: gray;">[ Add New
Product ]</a></td>
</tr>
<?php
$query = my_query("
SELECT chp.products_prod_id, p.prod_id, p.prod_name,
p.prod_no, p.prod_status
FROM categories_has_products as chp, products as p
WHERE chp.categories_cat_id = '".$cat_id."' AND
chp.products_prod_id = p.prod_id
", 0);
while($result = mysql_fetch_array($query))
{
?>
<tr>
<td align="left" valign="top">» <?= $result
['prod_name'] ?></td>
<td align="center" valign="top">[ <?= $result['prod_no'] ?> ]
</td>
<td align="center" valign="top">
<img src="../images/icon_edit2.gif" alt="EDIT"
width="14" height="14" hspace="0" vspace="0" border="0">
<a href="products.php?cat_id=<?= $cat_id ?>&cat_parent=<?
= $cat_parent ?>&action=delete&prod_id=<?= $result['prod_id'] ?>"
onclick="return confirm('Do you really want to delete this
product?');"><img src="../images/icon_delete.gif" alt="DELETE"
width="14" height="14" hspace="5" vspace="0" border="0"></a>
<?php
switch($result['prod_status'])
{
case 'live':
echo '<a href="products.php?
new_status=hidden&prod_id='.$result['prod_id'].'&cat_id='.
$cat_id.'&cat_parent='.$cat_parent.'"><img src="../images/
status_live.gif" alt="LIVE" width="13" height="13" hspace="2"
vspace="0" border="0"></a>';
break;
case 'hidden':
echo '<a href="products.php?
new_status=live&prod_id='.$result['prod_id'].'&cat_id='.
$cat_id.'&cat_parent='.$cat_parent.'"><img src="../images/
status_hidden.gif" alt="HIDDEN" width="13" height="13" hspace="2"
vspace="0" border="0"></a>';
break;
case 'temp':
echo '<b>[T]</b>';
break;
}
?>
</td>
</tr>
<?php } ?>
</table>
<?php
include 'includes/footer.php';
?>
Solution 3:
######################################################################
######################
<?php
require 'includes/header.php';
$cat_parent = $_GET['cat_parent'];
$cat_id = $_GET['cat_id'];
$content = '<table border="0" align="center" cellpadding="1"
cellspacing="0" style="border: 1px solid #EBEBEB; padding: 25px;">';
$content .= ' <tr>';
$content .= ' <td align="left" height="35" valign="top"
colspan="2"><b><u>'. $_SESSION['CATEGORIES']['name']
[$cat_parent] .' > '. $_SESSION['CATEGORIES']['name'][$cat_id] .'</
u></b></td>';
$content .= ' <td align="center" valign="bottom"
colspan="2"><a href="new_product.php?cat_id='.
$cat_id.'&cat_parent='.$cat_parent.'" style="font-size: 11px;
color: gray;">[ Add New Product ]</a></td>';
$content .= ' </tr>';
$query = my_query("
SELECT chp.products_prod_id, p.prod_id, p.prod_name,
p.prod_no, p.prod_status
FROM categories_has_products as chp, products as p
WHERE chp.categories_cat_id = '".$cat_id."' AND
chp.products_prod_id = p.prod_id
", 0);
while($result = mysql_fetch_array($query))
{
$content .= ' <tr>';
$content .= ' <td align="left" valign="top">» '.
$result['prod_name'] .'</td>';
$content .= ' <td align="center" valign="top">[ '. $result
['prod_no'] .' ]</td>';
$content .= ' <td align="center" valign="top">';
$content .= ' <img src="../images/icon_edit2.gif"
alt="EDIT" width="14" height="14" hspace="0" vspace="0" border="0">';
$content .= ' <a href="products.php?cat_id='.
$cat_id.'&cat_parent='.$cat_parent.'&action=delete&prod_id='.$result
['prod_id'].'" onclick="return confirm(\'Do you really want to
delete this product?\');"><img src="../images/icon_delete.gif"
alt="DELETE" width="14" height="14" hspace="5" vspace="0"
border="0"></a>';
switch($result['prod_status'])
{
case 'live':
$content .= '<a
href="products.php?new_status=hidden&prod_id='.$result
['prod_id'].'&cat_id='.$cat_id.'&cat_parent='.$cat_parent.'"><img
src="../images/status_live.gif" alt="LIVE" width="13" height="13"
hspace="2" vspace="0" border="0"></a>';
break;
case 'hidden':
$content .= '<a
href="products.php?new_status=live&prod_id='.$result
['prod_id'].'&cat_id='.$cat_id.'&cat_parent='.$cat_parent.'"><img
src="../images/status_hidden.gif" alt="HIDDEN" width="13"
height="13" hspace="2" vspace="0" border="0"></a>';
break;
case 'temp':
$content .= '<b>[T]</b>';
break;
}
$content .= ' </td>';
$content .= ' </tr>';
}
$content .= '</table>';
echo $content;
include 'includes/footer.php';
?>
I think third solution would be the best solution?
Thanks for any opinion.
-afan
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
Brent Baisley
Systems Architect
Landover Associates, Inc.
Search & Advisory Services for Advanced Technology Environments
p: 212.759.6400/800.759.0577
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php