比如,原来是这样显示:公司 编号 价格
Apple 4698 100
IBM 3532 200
微软 2356 300现在需要这样在页面上显示出来:公司 Apple IBM 微软
编号 4698 3532 2356
价格 100 200 300
不一定是现在这个三行三列的,可能是不固定的,比如20几行,也有可能。
还请各位帮忙 !谢谢——————————————————————————————————————找到的代码,但这样的话 :只能横向显示出一行数据
方法二. 将重复标记拖到<td>外(纵向重复是在<tr>外),加入一个变量, 每重复 1 次加 1, 如果%3等于 0 echo 一个"<tr>" . 请仔细看 <table width="100%" border="1" cellspacing="1" cellpadding="5">
<tr>
<?php $str=0;// 开始定义重复函数?>
<?php do { // 开始重复?>
<td>
<?php $str++;?>
<?php echo $str; ?>.<?php echo $row_recordset1['name']; ?>
</td>
<?php if ($str%3==0)echo "<tr>"; ?> <?php } while ($row_recordset1 = mysql_fetch_assoc($recordset1)); // 重复td内容?>
</tr>
Apple 4698 100
IBM 3532 200
微软 2356 300现在需要这样在页面上显示出来:公司 Apple IBM 微软
编号 4698 3532 2356
价格 100 200 300
不一定是现在这个三行三列的,可能是不固定的,比如20几行,也有可能。
还请各位帮忙 !谢谢——————————————————————————————————————找到的代码,但这样的话 :只能横向显示出一行数据
方法二. 将重复标记拖到<td>外(纵向重复是在<tr>外),加入一个变量, 每重复 1 次加 1, 如果%3等于 0 echo 一个"<tr>" . 请仔细看 <table width="100%" border="1" cellspacing="1" cellpadding="5">
<tr>
<?php $str=0;// 开始定义重复函数?>
<?php do { // 开始重复?>
<td>
<?php $str++;?>
<?php echo $str; ?>.<?php echo $row_recordset1['name']; ?>
</td>
<?php if ($str%3==0)echo "<tr>"; ?> <?php } while ($row_recordset1 = mysql_fetch_assoc($recordset1)); // 重复td内容?>
</tr>
你需要的是阵列转置!!!如果你使用数组缓存记录(大多数据库类都提供一个 retchall 可以直接使用)
while($row_recordset1 = mysql_fetch_row($recordset1)) {
$ret[] = $row;
}echo '<table>';
for($i=0; $i<count($ret[0]); $i++) {
echo '<tr>';
for($j=0; $j<count($ret); $j++)
echo "<td>{$[$j][$i]}</td>";
}
echo '</tr>';
}
echo '</table>';你也可以使用移动记录集指针的方法来实现
你好!谢谢你的答复 但现在出现这个问题:
Parse error: parse error, expecting `T_VARIABLE' or `'$''
echo "<td>{$[$j][$i]}</td>"; //改成echo "<td>{$ret[$j][$i]}</td>";