-4

我的数据库中有 88 行我正在从数据库中检索,我希望它们像这样在 32、30、26 等三列中显示它们,你能帮我怎么做吗?32,30,30 是每列应该有的行数

4

2 回答 2

1

If you want even columns, it will be easier to do (I cannot really understand hoe you came to 32,30,26) But you could do something like this:

$counter=1;
echo "<table><tr>"
while($row=someFetchFromDatabase($result))
{
    echo "<td>$row['col1']</td>";
    $counter++;
    if($counter%3==0)
    {
        echo "</tr><tr>";
    }
}
echo "</tr></table>";

This will increment a counter and display three rows until the end of the dataset - though you should add to the solution here to clean up the details.

于 2012-09-26T11:30:15.060 回答
1

Count the number of results mysql_num_rows(), divide by 3. In your php loop, add a counter. When it hits the result of the division, move onto the next column. Something like this:

$third = mysql_num_rows($sqlresult)/3;
$counter=1;
echo "<div>";
while($r = mysql_fetch_array($sqlresult)){
    if($counter++ > $third){
        $counter = 1;
        echo "</div><div>";
    }
    echo $r[0]."<br />";
}
echo "</div>";

You could also change the divs to tds and trs if you are using tables or remove the markup and use commas as you have done in your question

于 2012-09-26T11:31:04.110 回答