在表中显示查询结果 [英] Display query result in a table

查看:97
本文介绍了在表中显示查询结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a MySQL query with over 50 return results. Now I need to display the results in a table with 3 rows and 3 columns.

类似这样的东西:

<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尝试过:

I tried it with PHP like this:

$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;     
}

然后我像这样在表中打印内容数组:

Then I am printing the content array in my table like this:

<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个内容.我的问题是我想显示更多内容.我将使用分页来显示内容;诸如0-9、10-18、19-27等.

Using this code, I can only display 9 contents. My problem is that I want to display more content. I'm going to use pagination to display contents; something like 0-9, 10-18, 19-27 etc.

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

NOTE: I can do the pagination part.

我希望有人会为此给我正确的方向.

I hope someone will give me the right direction for this.

谢谢.

推荐答案

尝试类似的操作

 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查询. 例如:

this example will print in table all the result of the query. if you want to limit only to some results, so limit the sql query. for example:

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

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

to get each content,name, address in TD, and 3 of mycontent(content, name, address) in a TR try this:

$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>";

这篇关于在表中显示查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆