我有这个代码,假设显示$ 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)
答案 0 :(得分:0)
if ($fstate != $prev) {
echo "<tr>$fstate</tr><tr>";
您正在制作格式错误的表格。如果您的表格内容不在<td>
或<th>
标记内,则在呈现时,它们会显示在错误的位置。试试这个:
if ($fstate != $prev) {
echo "<tr><th>$fstate</th></tr><tr>";