If what you mean is seperating code from design have you tried in your while, if and foreach statements as well as your functions ending the php tag prematurly before adding the closing }
this is what i mean:
<?php
$SQL ...
while($row = mysql_fetch_array($result)) { ?>
Hello <?php $row[FirstName]; ?>, welcome to...
<img src="logo.png">
<?php } ?>
By the way, don't keep images in the database if you are intending to, it's bad! Especially for backing up. Rather chmod a directory to 777 and upload the images there.