before we can help, you really should start cleaning up your code.
Do some research into coding standards and methodologies - This will make coming back to your code, or someone else going through your code a much less painfull process:
< ?
$a = '0';
$filepath = "couples/$surname/gallery/thumbs";
$url_path = "couples/$surname/gallery";
$dir = dir($filepath);
echo "<table border='0' cellpadding='5' cellspacing='5' width='75%'>\n";
while($entry=$dir->read())
{
if($entry == "." || $entry == "..")
{
continue;
}
$fp = @fopen("$filepath/$entry","r");
$acceptable_nums = array('3','6','9','12','15','18','21','24','27','30','33','36','38','41','44','47');
for ($i=0; $i<sizeof($acceptable_nums); $i++)
{
if ($a == $acceptable_nums[$i])
{
echo "<tr>\n";
}
}
? >
<td>
<table width="1" border="0">
<tr>
<td align="center"><a href="< ? echo "$url_path/$entry" ? >"><img src="< ? echo "$filepath/$entry" ? >" alt="CLICK TO ENLARGE" border="0"></a></td>
</tr>
<tr>
<td align="center">
< ?
$remainder = substr($entry, 0, -4);
$remainder = str_replace("_"," ",$remainder);
echo "$remainder";
? >
</td>
</tr>
</table>
</td>
< ?
$a = $a + 1;
}
? >