在表中显示查询结果

时间:2013-03-16 14:16:29

标签: php mysql

我有一个MySQL查询,返回结果超过50。现在我需要在一个包含3行和3列的表中显示结果。

这样的事情:

<table>
    <tr>
        <td>Content</td>                    
        <td>Content</td>                    
        <td>Content</td>                                        
    </tr>   
    <tr>
        <td>Content</td>                    
        <td>Content</td>                    
        <td>Content</td>                                        
    </tr>   
    <tr>
        <td>Content</td>                    
        <td>Content</td>                    
        <td>Content</td>                                        
    </tr>
</table>

我用这样的PHP尝试过:

$q = "SELECT name, address, content FROM mytable"; 
$r = mysqli_query($dbc, $q); 

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
    $name   = $row['name'];
    $address = $row['address'];
    $content = $row['content'];

    //Create new output
    $output  = "<p>$name</p>";
    $output .= "<p>$address</p>";
    $output .= "<p>$content</p>";

    //Add output to array
    $mycontent[] = $output;     
}

然后我在我的表中打印内容数组,如下所示:

<tr>
    <td><?php echo $mycontent[0]; ?></td>                   
    <td><?php echo $mycontent[1]; ?></td>                   
    <td><?php echo $mycontent[2]; ?></td>                   
</tr>   
<tr>
    <td><?php echo $mycontent[3]; ?></td>                   
    <td><?php echo $mycontent[4]; ?></td>                   
    <td><?php echo $mycontent[5]; ?></td>                                       
</tr>   
<tr>
    <td><?php echo $mycontent[6]; ?></td>                   
    <td><?php echo $mycontent[7]; ?></td>                   
    <td><?php echo $mycontent[8]; ?></td>                                           
</tr>

使用此代码,我只能显示9个内容。我的问题是我想要显示更多内容。我将使用pagination来显示内容;像0-9,10-18,19-27等等。

  

注意:我可以做分页部分。

我希望有人会给我正确的方向。

谢谢。

4 个答案:

答案 0 :(得分:3)

尝试类似:

 echo "<table>";
 while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
 $name   = $row['name'];
 $address = $row['address'];
 $content = $row['content'];
 echo "<tr><td>".$name."</td><td>".$address."</td><td>".$content."</td></tr>";
 }
 echo "</table>";

此示例将在表中打印查询的所有结果。 如果要仅限制某些结果,请限制sql查询。 例如:

 $q = "SELECT name, address, content FROM mytable limit 50"; 

获取TR中的每个内容,名称,地址以及TR中的3个内容(内容,名称,地址),请尝试以下操作:

$c= mysql_query("select COUNT(name) from mytable");
$n=mysql_fetch_array($c);
$maxname= $n[0];
$i=0;
$tr=0;
echo "<table>";
while ($tr < $maxname)
{ 
echo "<tr>";
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC) and $i<$tr) {
$name   = $row['name'];
$address = $row['address'];
$content = $row['content'];
echo "<td>".$name." | ".$address." | ".$content."</td>";
$i++;
}
echo "</tr>";
$tr= $tr+3;
}
echo "</table>";

答案 1 :(得分:1)

尝试这样的事情。它将您的值放在一个易于使用的关联数组中。然后你就循环了。

<?php
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
    $values[] = array(
        'name' => $row['name'],
        'address' => $row['address'],
        'content' => $row['content']
    );
}
?>
<table>
<?php
foreach($values as $v){
    echo '
    <tr>
        <td>'.$v['name'].'</td>
        <td>'.$v['address'].'</td>
        <td>'.$v['content'].'</td>
    </tr>
    ';
}
?>
</table>

答案 2 :(得分:0)

使用for - 循环来迭代$mycontent - 数组:

编辑:我意识到$mycontent的构建方式与我最初假设的不同:

$h = "<table>"; // we are going to build the table in $h

$maxcount = count($mycontent); // get the number of values within the array 

for ($i = 0;$i < $maxcount;$i++) { // counting $i up from 0 to $maxcount -1 ...

   $h.= "<tr><td>{$mycontent[$i]}</td></tr>"; // build row

}

$h.= "</table>"; // close the table
echo $h; // echo it 

---&GT;现场演示:http://3v4l.org/g1E1T

答案 3 :(得分:0)

如果你计划在一个函数中使用它,你可以存储在一个变量中,或者你可以打印它。无论哪种方式......

$q = "SELECT name, address, content FROM mytable"; 
$r = mysqli_query($dbc, $q); 

$str = "<table>";

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
    $str .= "<tr>";
    $str .= "<td>" . $row['name'] . "</td>";
    $str .= "<td>" . $row['address'] . "</td>";
    $str .= "<td>" . $row['content'] . "</td>";
    $str .= "</tr>";
}

$str .= "</table>"

echo $str;

你去吧!