作者:乔山办公网日期:
返回目录:word文档
在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>
下面是从e799bee5baa6e4b893e5b19e332数据库读取代码:
<?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>
e79fa5e98193e78988e69d83331require_once 'PHPWord.php';
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
//定义样式数组
$styleTable = array(
'borderSize'=>6,
'borderColor'=>'006699',
'cellMargin'=>80
);
$styleFirstRow = array(
'borderBottomSize'=>18,
'borderBottomColor'=>'0000ff',
'bgColor'=>'66bbff'
);
//定义单元格样式数组
$styleCell = array('valign'=>'center');
$styleCellBTLR = array('valign'=>'center','textDirection'=>PHPWord_Style_Cell::TEXT_DIR_BTLR);
//定义第一行的字体
$fontStyle = array('bold'=>true,'align'=>'center');
//添加表格样式
$PHPWord->addTableStyle('myOwnTableStyle',$styleTable,$styleFirstRow);
//添加表格
$table = $section->addTable('myOwnTableStyle');
在相应的元素的class中给定在知CSS文件中定义不同的样式类道
例如:
1.样式文件style.css
...
.Style1{background:#000;}
.Style2{background:#FFF;}
...
2.程序文件(php)
<table>
<?php
for($i = 0; $i<10; $i++)
{
if($i%2!=0 && $i=!0)
{
?>
<tr class="Style1">
<td></td>
</tr>
<?php
}
else
{
?>
<tr class="Style2">
<td></td>
</tr>
<?php
}
}
?>
</table>
============================
那样只要判断一下就专可以了
判断一下输出为空的情况,什么都不输出
0回答者:属 ichigo3836
require_once 'PHPWord.php';
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
//定义样式数组e79fa5e98193e4b893e5b19e363
$styleTable = array(
'borderSize'=>6,
'borderColor'=>'006699',
'cellMargin'=>80
);
$styleFirstRow = array(
'borderBottomSize'=>18,
'borderBottomColor'=>'0000ff',
'bgColor'=>'66bbff'
);
//定义单元格样式数组
$styleCell = array('valign'=>'center');
$styleCellBTLR = array('valign'=>'center','textDirection'=>PHPWord_Style_Cell::TEXT_DIR_BTLR);
//定义第一行的字体
$fontStyle = array('bold'=>true,'align'=>'center');
//添加表格样式
$PHPWord->addTableStyle('myOwnTableStyle',$styleTable,$styleFirstRow);
//添加表格
$table = $section->addTable('myOwnTableStyle');