Keeping everting in correct table cells

Hi
This is hard to explain but I’ll try I am designing a booking system for my business, which will look something like this:
say with each hour cell ie 9am next to it split 2 cells with 9:00 & bottom cell 9:30

I have two loops one that runs the 7days a week and inside that the hours of the day, if I have one cell per hour its easy to fill it in as its all in order,
all the TD and TR are in the right order but with a spilt row the data is all over the place. How can i get the data in the correct cells when I am using loops to build the hours and days. Any help or ideas would be great. Main code I am using is at bottom

<table width=“600” border=“1” cellpadding=“0” cellspacing=“0”>
<tr>
<td> </td>
<td>28th April </td>
<td>29th April</td>
<td>30th April </td>
</tr>
<tr>
<td rowspan=“2”>9am</td>
<td>9:00am</td>
<td rowspan=“2”>empty</td>
<td>8:30am 2 </td>
</tr>
<tr>
<td>9:30am</td>
<td>9:30am 2 </td>
</tr>
<tr>
<td>10am</td>
<td>booked</td>
<td>booked</td>
<td>booked</td>
</tr>
</table>

if($book==Y)
{
$query = “INSERT INTO tblTimeSlot SET slotID=‘123’, slotStart=‘$slotStart’, slotFinish=‘$slotFinish’, status=‘Booked’”;
mysql_query($query,$db);
}

//variables
$hourStart = 9;
$hourFinish = 17;

$slotDuration = 25;
$slotsPerHour = 60/$slotDuration;
$slotsPerHour = round($slotsPerHour,2);

echo $slotsPerHour;

if($nextWeek!=“”)
{
$date = strtotime(“$day-$month-$year GMT”);
$todayNow = $date + 604800;
echo $todayNow;
}
else
{
$todayNow = time();
}

$dateDay = date(‘D’, $todayNow) ;
$dateNum = date(‘j’, $todayNow) ;
$dateYear = date(‘Y’, $todayNow);
$dateMonth = date(‘n’, $todayNow);

?>
</p>
<p> </p>
<?
echo(“<a href=‘/test5.php?nextWeek=Y&day=$dateNum&month=$dateMonth&year=$dateYear’>Next Week</a>”);?>
<table width=“900” border=“1” cellpadding=“0” cellspacing=“0”>
<tr><td>time</td>
<?
// create top row of days for current timr
for($i =0; $i<7; $i++)
{
$time = 86400 * $i;
$newTime = $todayNow + $time;

$dateDay = date('D', $newTime) ;
$dateNum = date('j', $newTime) ;
echo("&lt;td&gt;$dateDay $dateNum&lt;/td&gt;");

}

?>
</tr>

<?
//$todayNow = time();

$dateDay = date(‘D’, $todayNow) ;
$dateNum = date(‘j’, $todayNow) ;
$dateYear = date(‘Y’, $todayNow);
$dateMonth = date(‘n’, $todayNow);

// hourly time slot rows + needs 7 vertical columns for time
for($h=$hourStart; $h<=$hourFinish; $h++)
{
echo(“<tr>”);
// columns 7 for each day
for($c = 0; $c <7; $c++)
{

		$time = 86400 * $c;
		$newTime = $todayNow + $time;
		$dateNum = date('j', $newTime);
		
		$dateYear = date('Y', $newTime);
		$dateMonth = date('M', $newTime);
		
		// build up date and time from $h
		$timeStamp = "$dateNum $dateMonth $dateYear $h:00 GMT";
		$unix = strtotime($timeStamp);
		
		// display left column time //
		if($c==0)
			{
			// display hour column on left
			if($h&gt;12 && $h&lt;=23)
				{
				$columnTime = $h - 12;
				$columnTime = $columnTime ."pm";
				}
			else
				{
				$columnTime = $h."am";
				}
			
			echo("&lt;td&gt;$columnTime&lt;/td&gt;");
			}
			// end of column time //
			
			
		    // check booking and display on screen
			$query = "SELECT status FROM tblTimeSlot WHERE slotStart='$unix'";
			$result = mysql_query($query,$db);
			if(!$row = mysql_fetch_array($result))
				{
				$showTime = gmdate("g:ia",$unix);
				$slotFinish = $unix+(60*$slotDuration);
				echo("&lt;td&gt;$showTime&lt;br&gt;
				&lt;a href='/test6.php?book=Y&slotStart=$unix&slotFinish=$slotFinish'&gt;$unix&lt;/a&gt;&lt;/td&gt;");
				}
			else
				{
				$status = $row[status];
				echo("&lt;td&gt;$status&lt;/td&gt;");
				}
		
		
		
		}
echo("&lt;/tr&gt;");
}
?&gt;

</table>

Your table needs to know how many rows there will be to begin with.

It looks like you’re trying to do a week-calendar grid, with half-hour increments.
#1: Dont do individual queries for the status. Pull all the records for the week in question (WHERE slotStart BETWEEN X and Y). Dump the results into a keyed array. And you can reference check them.
#2: Loop restructuring, simplification. You’re doing wayyy too much math here.

Pseudo-Pseudocode:
res = mysql_query(Select query here)
while row = mysql_fetch_assoc(res) {
bookings[row[slotStart]] = row[status];
}
timestamp = strtotime(startdateatmidnight)
<table><tr><td></td>
for(day = 0; day < 7; day++) {
echo <td>.date(‘jS F’,timestamp+(day * 86400)).</td>; //Header row
}
</tr>
for(hour = 0; hour < 24; hour++) {
echo <tr><td rowspan=‘2’>.date(‘g:00 A’,timestamp+(hour * 3600)).</td> //Time is consistant per week.
for(day = 0; day < 7; day++) {
echo <td>
if(isset(bookings[(timestamp+(date * 86400)+(hour * 3600))]) echo bookings[(timestamp+(date * 86400)+(hour * 3600))]
else echo “Not Booked” //Or whatever.
echo </td>
} //Done with the ‘on the hour’ booking row.
echo </tr><tr>
for(day = 0; day < 7; day++) {
echo <td>
if(isset(bookings[(timestamp+(date * 86400)+(hour * 3600)+1800)]) echo bookings[(timestamp+(date * 86400)+(hour * 3600)+1800)]
else echo “Not Booked” //Or whatever.
echo </td>
} //Done with the ‘XX:30’ booking row.
echo </tr>
}
</table>