优化下表的最佳方法是什么?
// Monday period 1
echo '
<td class="gridSide">Mon</td>
<td class="box" id="grid2_mon1">';
$sql = "
SELECT
COUNT(*) totalCount
FROM
ts_room
WHERE
NOT EXISTS (
SELECT 1
FROM ts_roompref
JOIN ts_request
ON ts_roompref.request_id = ts_request.roompref_id
AND day_id = 1
AND period_id = 1
WHERE
ts_room.id = ts_roompref.room_id)
";
$stm = $pdo->prepare( $sql );
$stm->execute( array( ':room_id' => $room) );
$rows = $stm->fetchColumn();
echo $rows.'<br>free';
echo '</td>';
// Monday period 2
echo '
<td class="box" id="grid2_mon2">';
$sql = "
SELECT
COUNT(*) totalCount
FROM
ts_room
WHERE
NOT EXISTS (
SELECT 1
FROM ts_roompref
JOIN ts_request
ON ts_roompref.request_id = ts_request.roompref_id
AND day_id = 1
AND period_id = 2
WHERE
ts_room.id = ts_roompref.room_id)
";
$stm = $pdo->prepare( $sql );
$stm->execute( array( ':room_id' => $room) );
$rows = $stm->fetchColumn();
echo $rows.'<br>free';
echo '</td>';
// Monday period 3
echo '
<td class="box" id="grid2_mon3">';
$sql = "
SELECT
COUNT(*) totalCount
FROM
ts_room
WHERE
NOT EXISTS (
SELECT 1
FROM ts_roompref
JOIN ts_request
ON ts_roompref.request_id = ts_request.roompref_id
AND day_id = 1
AND period_id = 3
WHERE
ts_room.id = ts_roompref.room_id)
";
$stm = $pdo->prepare( $sql );
$stm->execute( array( ':room_id' => $room) );
$rows = $stm->fetchColumn();
echo $rows.'<br>free';
echo '</td>';
// Monday period 4
echo
'<td class="box" id="grid2_mon4">';
$sql = "
SELECT
COUNT(*) totalCount
FROM
ts_room
WHERE
NOT EXISTS (
SELECT 1
FROM ts_roompref
JOIN ts_request
ON ts_roompref.request_id = ts_request.roompref_id
AND day_id = 1
AND period_id = 4
WHERE
ts_room.id = ts_roompref.room_id)
";
$stm = $pdo->prepare( $sql );
$stm->execute( array( ':room_id' => $room) );
$rows = $stm->fetchColumn();
echo $rows.'<br>free';
echo '</td>';
// Monday period 5
echo '
<td class="box" id="grid2_mon5">';
$sql = "
SELECT
COUNT(*) totalCount
FROM
ts_room
WHERE
NOT EXISTS (
SELECT 1
FROM ts_roompref
JOIN ts_request
ON ts_roompref.request_id = ts_request.roompref_id
AND day_id = 1
AND period_id = 5
WHERE
ts_room.id = ts_roompref.room_id)
";
$stm = $pdo->prepare( $sql );
$stm->execute( array( ':room_id' => $room) );
$rows = $stm->fetchColumn();
echo $rows.'<br>free';
echo '</td>';
// Monday period 6
echo '
<td class="box" id="grid2_mon6">';
$sql = "
SELECT
COUNT(*) totalCount
FROM
ts_room
WHERE
NOT EXISTS (
SELECT 1
FROM ts_roompref
JOIN ts_request
ON ts_roompref.request_id = ts_request.roompref_id
AND day_id = 1
AND period_id = 6
WHERE
ts_room.id = ts_roompref.room_id)
";
$stm = $pdo->prepare( $sql );
$stm->execute( array( ':room_id' => $room) );
$rows = $stm->fetchColumn();
echo $rows.'<br>free';
echo '</td>';
// Monday period 7
echo '
<td class="box" id="grid2_mon7">';
$sql = "
SELECT
COUNT(*) totalCount
FROM
ts_room
WHERE
NOT EXISTS (
SELECT 1
FROM ts_roompref
JOIN ts_request
ON ts_roompref.request_id = ts_request.roompref_id
AND day_id = 1
AND period_id = 7
WHERE
ts_room.id = ts_roompref.room_id)
";
$stm = $pdo->prepare( $sql );
$stm->execute( array( ':room_id' => $room) );
$rows = $stm->fetchColumn();
echo $rows.'<br>free';
echo '</td>';
// Monday period 8
echo '
<td class="box" id="grid2_mon8">';
$sql = "
SELECT
COUNT(*) totalCount
FROM
ts_room
WHERE
NOT EXISTS (
SELECT 1
FROM ts_roompref
JOIN ts_request
ON ts_roompref.request_id = ts_request.roompref_id
AND day_id = 1
AND period_id = 2
WHERE
ts_room.id = ts_roompref.room_id)
";
$stm = $pdo->prepare( $sql );
$stm->execute( array( ':room_id' => $room) );
$rows = $stm->fetchColumn();
echo $rows.'<br>free';
echo '</td>';
// Monday period 9
echo '
<td class="box" id="grid2_mon9">';
$sql = "
SELECT
COUNT(*) totalCount
FROM
ts_room
WHERE
NOT EXISTS (
SELECT 1
FROM ts_roompref
JOIN ts_request
ON ts_roompref.request_id = ts_request.roompref_id
AND day_id = 1
AND period_id = 2
WHERE
ts_room.id = ts_roompref.room_id)
";
$stm = $pdo->prepare( $sql );
$stm->execute( array( ':room_id' => $room) );
$rows = $stm->fetchColumn();
echo $rows.'<br>free';
echo '</td>';
}