The example below is pretty simple. Draw a table, but populate the rows with data returned from a MySQL query. I'd just like to know how you would go about separating this code out, so the HTML does not sit with the PHP. I can see various approaches, but I don't know which would be best. I have been simply writing echo statements and wrapping the entire file contents in PHP tags, but that keeps the HTML with the PHP, which isn't best code practice.
<table>
<tr>
<th>Date</th>
<th>Title</th>
<th>News</th>
</tr>
<?php
$sql = "SELECT Date, Title, NewsItem FROM News";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result))
{
$html = "<tr>";
$html .= "<td>" . $row[Date] . "</td>";
$html .= "<td>" . $row[Title] . "</td>";
$html .= "<td>" . $row[NewsItem] . "</td>";
$html .= "</tr>";
printf($html);
}
?>
</table>

New Topic/Question
Reply



MultiQuote





|