在html表格中打印数据库数据 [英] Print database data in html Table

查看:109
本文介绍了在html表格中打印数据库数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何在html表格上打印来自数据库的数据,但是我有一个我无法理解的任务,而且我做不到。
我在数据库中有一个表格,当我选择这些数据时,我希望它们在html表格上显示,但是像这样:

图片http://img683.imageshack.us/img683/9350/tablea.png

解决方案

为了实现这样的表格布局,您只需要调整表格行结束的位置。

 < table> 
< tr>
<?php
$ count = 0;
while($ row = mysql_fetch_assoc($ result)):
$ count ++;
?>
< td><?php echo $ row ['value']?>< / td>
<?php if($ count%2 == 0 || $ count == mysql_num_rows($ res)):?>
< / tr>
<?php if($ count!= mysql_num_rows($ result)):?>
< tr>
<?php endif; ?>
<?php endif; ?>
<?php endwhile; ?>
< / table>

上面做的是使用模数运算符(

另外,如果你想改变它,你可以使用$ c $来计算除法的余数)来打印一个结束和打开的行标记。您的表格布局为3或4列宽,您只需更改应用于模数的数字即可:

  $ count %3 == 0 // 3列
$ count%4 == 0 // 4列等


I know how to print data from database on html table but i have one task that i can not understand, and i can not do it. I have a table in database and when i select those data i want them to display on html table but something like this:

Image http://img683.imageshack.us/img683/9350/tablea.png

解决方案

To achieve a table layout like that you just need to condition the placement of the end of your table rows.

<table>
  <tr>
<?php
  $count = 0;
  while($row = mysql_fetch_assoc($result)) :
    $count++;
?>
    <td><?php echo $row['value'] ?></td>
<?php if($count % 2 == 0 || $count == mysql_num_rows($res)) : ?>
  </tr>
<?php if($count != mysql_num_rows($result)) : ?>
  <tr>
<?php endif; ?>
<?php endif; ?>
<?php endwhile; ?>
</table>

What the above does is use the modulus operator (%, calculates the remainder from division) to print a closing and opening row tag whenever we're at an evenly numbered result.

Also, if you wanted to change your table layout to be 3 or 4 columns wide all you need to do is change the number applied to the modulus:

$count % 3 == 0 //3 columns
$count % 4 == 0 //4 columns, etc

这篇关于在html表格中打印数据库数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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