instead of echoing a whole bunch of html you can close your php tags and just write html as normal.
for example:
<?php
...
...
...
foreach ($posts as $post) {
?>
<!-- here goes the html -->
<?php
}
?>
On the otherhand you mention in your question that you are trying to display html based on a page name.
You can do this by making a page template