每行HTML和PHP 5个状态

时间:2013-04-04 20:56:37

标签: php html count rows

我想要列出州和其下的城市。我希望每行有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>";
}

1 个答案:

答案 0 :(得分: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

这会创建一个像这样的表格 example snapshot