Assist cleaning up messy HTML/PHP code I really hate messy code, but I seem to be rolling in it. I just want to see if anyone has some helpful advice on how to tidy this up.

&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;th&gt;Zona&lt;/th&gt;
&lt;th&gt;Baptismos Mulheres&lt;/th&gt;
&lt;th&gt;Conf. Mulheres&lt;/th&gt;
&lt;th&gt;Batismos Homems&lt;/th&gt;
&lt;th&gt;Conf. Homems&lt;/th&gt;
&lt;/tr&gt;
&lt;?php
$currWeek = $CONF-&gt;getSetting('current_week');
$zones = $PDO-&gt;query("SELECT * FROM zones WHERE active = 1");
foreach($zones as $zone) {
$zoneName = $zone['name'];
$zoneUid = $zone['uid'];
?&gt;
&lt;tr&gt;
&lt;td&gt;&lt;?php print $zoneName; ?&gt;&lt;/td&gt;
&lt;/tr&gt;                   
&lt;?php

$areas = $PDO-&gt;query("SELECT * FROM areas WHERE zone = '".$zoneUid."'");
foreach($areas as $area) {
$areaName = $area['name'];
$areaUid = $area['uid'];
?&gt;
&lt;tr&gt;
&lt;td&gt;&lt;?php print $areaName; ?&gt;

&lt;?php


$missionaries = $PDO-&gt;query("SELECT missionary FROM missionary_areas WHERE semana = '".$currWeek."' AND area_uid = '".$areaUid."'");

$compArr = array();

foreach($missionaries as $missionary) {
$companions = $PDO-&gt;query("SELECT CONCAT(title, ' ', mission_name) as name from missionarios WHERE mid = '".$missionary['missionary']."'");


foreach($companions as $comp) {
$compArr[] = $comp['name'];
}

}

$compList = $CONF-&gt;commaSeparate($compArr);
?&gt;
&lt;?php print $compList; ?&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;?php
}
}

?&gt;


I am still a bit inexperienced and would like some helpful tips. My MySQL directory stucture is necessaraly crazy so I have to do a bunch of queries to get some simple data. Using PDO is helping quite a bit though.

Edit:

Database structure
WKS is right, first of all you should split your layout from your logic and your logic from your database query. (see Model-View-Controller-Pattern)

Lets start with your model (Model.php): (actually you could split it in a model for each entity/database table)

&lt;?php
class Model()
{
     public function __construct(PDO $connection)
     {
         $this-&gt;connection=connection;
     }

     public function getZones($week)  //find a nicer name
     {
         //... see Peters post for a sample join
         // prepare multi dimensional array as needed in your template
         return $zones;
     }
}


It is in general a bad idea to run SQL queries in a loop, so try to create one join for all your queries.

Now the simple Controller (index.php)

&lt;?php
require 'Model.php';
$connection=new PDO(...);
$model=new Model($connection);
$currentWeek = ...;
$zones= $model-&gt;getZones($currentWeek);

include "view.php"


That's it! Nothing fancy. Now the view:

&lt;table&gt;
    &lt;tr&gt;
         &lt;th&gt;Zona&lt;/th&gt;
         &lt;th&gt;Baptismos Mulheres&lt;/th&gt;
         &lt;th&gt;Conf. Mulheres&lt;/th&gt;
         &lt;th&gt;Batismos Homems&lt;/th&gt;
         &lt;th&gt;Conf. Homems&lt;/th&gt;
    &lt;/tr&gt;
    &lt;?php foreach($zones as $zone):?&gt;
    &lt;tr&gt;
         &lt;td&gt;&lt;?php echo $zone['name']; ?&gt;&lt;/td&gt;
         &lt;td colspan="4"&gt;&lt;/td&gt;
    &lt;/tr&gt;                   
        &lt;?php foreach($zones['areas'] as $area):?&gt;
        &lt;tr&gt;
            &lt;td&gt;
                 &lt;?php $area['name']; ?&gt; &lt;?php echo $area['companions']; ?&gt;
            &lt;/td&gt;
            &lt;td colspan="4"&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;?php endforeach?&gt;
    &lt;?php endforeach?&gt;
&lt;/table&gt;


Don't use all-capital-letter variable names. And don't use global variables. Both is considered bad practice.