Re: better way to mix html and php code?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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">&raquo; '. $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">&raquo; <?= $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">&raquo; '. $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

#3 by far. I used your #1 and #2 before discovering that #3 is far simpler to get it right the first time and far simpler to move stuff around for the rendered page. Also has the advantage of sending one complete stream to the client. No need to use buffering to optimize performance.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux