0

我想要一份州列表和它下面的城市。我希望每行有 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>";
}
4

1 回答 1

0

根据您当前的代码尝试,这是一种方法。该州及其所有城市都在同一个单元格内。

echo "<table>";
     $i = 1;
     $cols = 3;  // of cells per row
     $prev = '';
echo "<tr>";   // start the first row  
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) {

            if($i>$cols){  // check if we have reached our # of $cols
                echo "</td></tr><tr>";  // end cell/row and start new row
                $i=1; //reset counter
            }
            elseif ($prev != '') {  // if not the first cell/state
                echo "</td>";  // end cell before starting new cell/state
            }
            echo "<td><h2>$fstate</h2>";
            $prev = $fstate;
            $i++;
        }


        echo "<a href='browseresults.php?city=$fcity&state=$fstate'>$fcity, $fstate ($fcitycount)</a><br />";


}
echo "</td></tr>"; //end the last cell/row
echo "</table>";

通过更改$cols值(即$cols=5,您可以更改每行的单元格数。
请参阅 phpfiddle 中的示例 - http://phpfiddle.org/main/code/pkg-90i

这会创建一个像这样的表 -
示例快照

于 2013-04-05T02:38:07.073 回答