我的代码包含两列,如下所示:
社区 1
餐厅 1
社区 1
餐厅 2
社区 1
餐厅 3
我想要:
邻里 1
餐厅 1
餐厅 2
餐厅 3
$result = mysql_query("SELECT ID,RName,NHood FROM Restaurants ORDER BY NHood ASC, TRIM(LEADING 'The ' FROM RName) ASC",$connect);
$numRows = mysql_num_rows($result);
$middleIndex = (int)(($numRows+1) / 2);
$names = array();
while($row = mysql_fetch_assoc($result)) {
$names[] = $row['RName'];
$id[] = $row['ID'];
$hood[] = $row['NHood'];
}
// print the left column
echo "<table>";
echo "<tr>";
echo "<td width=60%>";
echo "<div id=\"left\">\n";
for($i = 0; $i < $middleIndex; $i++) {
echo $hood[$i];
echo "<p><a href=\"Restaurant.php?ID=".$id[$i]."\">$names[$i]</a></p>\n";
}
echo "</div>\n";
echo "</td>";
// print the right column
echo "<td>";
echo "<div id=\"right\">\n";
for($i = $middleIndex; $i < $numRows; $i++) {
echo $hood[$i];
echo "<p><a href=\"Restaurant.php?ID=".$id[$i]."\">$names[$i]</a></p>\n";
}
echo "</div>\n";
echo "</tr>";
echo "</table>";