在不同的表中显示检索数据

时间:2014-11-21 00:42:13

标签: php html

好的,我想要的是在表格数据库中显示检索到的数据,我希望我的表限制为每个表5个数据。我需要水平显示表格。

像这样:

while($row1 = sqlFetchArray($row)) 
{   $ctr = 0;
$ctr+1;
if($ctr=1 || $ctr<=5)
{
$html .='<table width="100px" style="float:left;"><tr><td>'.$row1['id'].'</td></tr></table>';
}
if($ctr=6 || $ctr<=10)
{
$html .='<table width="110px" style="float:left;"><tr><td>'.$row1['id'].'</td></tr></table>';
}
if($ctr=11 || $ctr<=15)
{
$html .='<table width="120px" style="float:left;"><tr><td>'.$row1['id'].'</td></tr></table>';
}
}

Output:
1    6
2    7
3    8
4    9
5    10

1 个答案:

答案 0 :(得分:1)

这应该适合你:

(但函数sqlFetchArray就像mysql_fetch_array一样工作)

<?php

    $limit = 15;

    for($numberCounter = 0; $numberCounter < $numberCount = mysql_num_rows($row); $numberCounter++) {

        $count  = 0;

        if($numberCounter >= mysql_num_rows($row))
            break;

        if ($count == 0 || $count % $limit == 0)
            echo "<table width='100px' style='float:left;'>"; 

        while ($row1 = sqlFetchArray($row) && $count < $limit) {
            if($numberCounter >= mysql_num_rows($row))
                break;

            echo "<tr><td>" . $row1[$numberCounter] . "</td></tr>";
            $count++;   
            $numberCounter++;
        }

        if($count == 0 || $count % $limit == 0)
            echo "</table>";

    }

?>

举个例子:

<?php

    $test = range(1, 43);
    $limit = 15;

    for($numberCounter = 0; $numberCounter < $numberCount = count($test); $numberCounter++) {

        $count  = 0;

        if($numberCounter >= count($test))
            break;

        if ($count == 0 || $count % $limit == 0)
            echo "<table width='100px' style='float:left;'>"; 

        while ($count < $limit) {
            if($numberCounter >= count($test))
                break;

            echo "<tr><td>" . $test[$numberCounter] . "</td></tr>";
            $count++;   
            $numberCounter++;
        }

        if($count == 0 || $count % $limit == 0)
            echo "</table>";

    }

?>