php輸出表格的實現代碼(修正版)
更新時間:2010年12月29日 11:40:00 作者:
在php開發(fā)過程中,對于大量的數據考慮用表格輸出的時候可以用得到,但部分朋友可能不是很清楚原理所以這里為大家準備了代碼。希望對學習php的朋友有所幫助。
網上的代碼很多都是錯誤的,腳本之家特修正了下。
<html>
<head>
<title>二行5列一共10個數據</title>
</head>
<body>
<table border="1" width=80%>
<tr>
<?php
$num = 5; //當前每一行顯示列數
$k = 1; //初始化
while($k<=10)
{
if($k % $num == 0){
if($k==10){
echo '<td>'.$k.'</td></tr>'."\r\n";
}else{
echo '<td>'.$k.'</td></tr><tr>'."\r\n";
}
}
else {
echo '<td>'.$k.'</td>'."\r\n";
}
$k+=1; //自加
}
?>
</table>
</body>
</html>
下面是從數據庫讀取的:
<?php
$sql = "select * from table ";
$query = mysql_query($sql);
$num = mysql_num_rows($query);
for($i=0;$i <$num;$i++) {
$result = mysql_fetch_array($query);
if($i%3==0){
$str.= " <tr style= 'word-break:break-all '> ";
}
$str.= " <td> ".$result[ "title "]. " </td> ";
if(($i+1)%$num==0) {
$str.= " </tr> ";
}
}
?>
<table border=0>
<?
echo $str;
?>
</table>
復制代碼 代碼如下:
<html>
<head>
<title>二行5列一共10個數據</title>
</head>
<body>
<table border="1" width=80%>
<tr>
<?php
$num = 5; //當前每一行顯示列數
$k = 1; //初始化
while($k<=10)
{
if($k % $num == 0){
if($k==10){
echo '<td>'.$k.'</td></tr>'."\r\n";
}else{
echo '<td>'.$k.'</td></tr><tr>'."\r\n";
}
}
else {
echo '<td>'.$k.'</td>'."\r\n";
}
$k+=1; //自加
}
?>
</table>
</body>
</html>
下面是從數據庫讀取的:
復制代碼 代碼如下:
<?php
$sql = "select * from table ";
$query = mysql_query($sql);
$num = mysql_num_rows($query);
for($i=0;$i <$num;$i++) {
$result = mysql_fetch_array($query);
if($i%3==0){
$str.= " <tr style= 'word-break:break-all '> ";
}
$str.= " <td> ".$result[ "title "]. " </td> ";
if(($i+1)%$num==0) {
$str.= " </tr> ";
}
}
?>
<table border=0>
<?
echo $str;
?>
</table>
相關文章
How do I change MySQL timezone?
The MySQL timezone is set to MST (-7 hours GMT/UTC) and is not configurable by you. MySQL is only capable of having 1 timezone setting per mysql daemon. Therefore, you cannot select NOW() and expect a result in a timezone other than MST.2008-03-03