我有这段代码假设显示$fstate,然后在它下面显示所有$fcity。它显示 $fcity 3 次,然后创建一个新列。我的问题是我可以让州和城市显示,但我不能让城市在适当的状态下显示。现在它显示所有州,然后显示它下面的城市。这是代码:
<?php
$fquery = "SELECT state, city, count(city) as num FROM needs WHERE country='$usercountry' AND status='posted' GROUP BY state, city ORDER BY state, city";
if ($result = mysql_query($fquery)) {
$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>$fstate</tr><tr>";
$prev="$fstate";
}
echo "<td><a href='node/browseresults.php?city=$fcity&state=$fstate'>$fcity, $fstate ($fcitycount)</a> </td>";
echo ($i < $num_rows) ? ((($i % $cols) == 0) ? '</tr>' : '') : '';
$i++;
}
echo "</table>";
}
?>
这就是我现在得到的(示例):
ARMOSCTNTX
Brookland, AR (1) Fayetteville , AR (2) Harrisburg, AR (2)
Hot Springs, AR (1) Jonesboro, AR (1) Searcy, AR (2)
St Louis, MO (3)
murrells inlet, SC (1) Myrtle Beach, SC (1)
Memphis, TN (1)
Arlington, TX (1)
但我需要它看起来像这样:
AR
Brookland, AR (1) Fayetteville , AR (2) Harrisburg, AR (2)
Hot Springs, AR (1) Jonesboro, AR (1) Searcy, AR (2)
MO
St Louis, MO (3)
SC
murrells inlet, SC (1) Myrtle Beach, SC (1)
TN
Memphis, TN (1)
TX
Arlington, TX (1)