我想要一份州列表和它下面的城市。我希望每行有 5 个州,其下有记录的城市,然后我希望它开始新的一行。我开始试图弄清楚,我已经走了几个小时,我的大脑很痛。任何人都可以帮忙吗?
这是我现在拥有的:
$lookup = "SELECT state, city, count(city) as num FROM needs WHERE country IS NULL AND
status='posted' GROUP BY state, city ORDER BY state, city";
if ($result = mysql_query($lookup)) {
$num_rows = mysql_num_rows($result);
echo "<table>";
$i = 1;
$cols = 3;
$prev = '';
while ($frows = mysql_fetch_array($result)) {
$fcity = $frows['city'];
$fstate = $frows['state'];
$fcitycount = $frows['num']; // num is holding your count by city
if ($fstate != $prev) {
echo "<tr><td><h2>$fstate</h2></td>";
$prev = $fstate;
}
echo "<tr><td><a href='browseresults.php?city=$fcity&state=$fstate'>$fcity, $fstate ($fcitycount)</a></td></tr>";
}
echo "</table>";
}
编辑:
它应该如下所示:
Arkansas Tennessee Missouri
Searcy, AR (1) Bartlett, TN (1) St. Louis, MO (4)
Little Rock, AR (4) Memphis, TN (3) Perry, MO (3)
Benton, AR (2) Branson, MO (1)
Shell, MO (2)
除了它会做 5 次而不是 3 次
编辑2:
这是我尝试过的代码,但仍然无法正常工作:-/
$lookup = "SELECT state, city, count(city) as num FROM needs WHERE country IS NULL AND status='posted' GROUP BY state, city ORDER BY state, city";
if ($result = mysql_query($lookup)) {
$num_rows = mysql_num_rows($result);
echo "<table><td>";
$i = 1;
$j = 1;
$cols = 5;
$prev = '';
while ($frows = mysql_fetch_array($result)) {
$fcity = $frows['city'];
$fstate = $frows['state'];
$fcitycount = $frows['num']; // num is holding your count by city
if ($fstate != $prev) {
echo "<tr><h2>$fstate</h2></tr>";
$prev = $fstate;
}
echo "<tr><a href='browseresults.php?city=$fcity&state=$fstate'>$fcity, $fstate ($fcitycount)</a><br></tr>";
if ($fstate != $prev) {
echo "</td><td>";
}
}
echo "</td></table>";
}