<?php
$say = array("ann","brenda","charles","david",
"edward","florence","geoff","harry",
"ingrid","james","kelly","liam");
$columns = 5;
for ($p=0; $p<count($say); $p++) {
// Start of table or line?
if ($p==0) { // Start of table
print "<table border=0><tr>";
} elseif ($p%$columns == 0) { // Start of row
print "<tr>";
}
print "<td>".htmlspecialchars($say[$p])."</td>";
// End of table or line?
if (($p+1)%$columns == 0) { // End of row
print "</tr>";
}
if ($p==count($say)-1) { // End of table
$empty = $columns - (count($say)%$columns) ;
if ($empty != $columns) {
print "<td colspan=$empty> </td>";
}
print "</tr></table>";
}
}
?>
结果:
ann brenda charles david edward
florence geoff harry ingrid james
kelly liam
我正在尝试对 mysql 做同样的事情 到目前为止我得到了
<?php
$con = mysql_connect("localhost","root","lol");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$result = mysql_query("SELECT * FROM test");
while($row = mysql_fetch_array($result))
{
$id=$row['id'];
$nam=$row['nam'];
$columns = 3;
for ($p=0; $p<count($id); $p++) {
// Start of table or line?
if ($p==0) { // Start of table
print "<table border=0><tr>";
} elseif ($p%$columns == 0) { // Start of row
print "<tr>";
}
print "<td>".$nam."</td>";
// End of table or line?
if (($p+1)%$columns == 0) { // End of row
print "</tr>";
}
if ($p==count($nam)-1) { // End of table
$empty = $columns - (count($nam)%$columns) ;
if ($empty != $columns) {
print "<td colspan=$empty> </td>";
}
print "</tr></table>";
}
}
}
mysql_close($con);
?>
结果:
ann
brenda
charles
david
edward
florence
geoff
harry
ingrid
james
kelly
liam
问题:怎么了?
数据库表
id nam
1 ann
2 brenda
3 charles
4 david
5 edward
6 florence
7 geoff
8 harry
9 ingrid
10 james
11 kelly
12 liam
最佳答案
我建议您将代码分成两个不同的函数。
一个函数将从数据库或数组中读取信息,另一个函数将格式化输出。
现在,看起来非常像您将第一 block 代码放入第二段代码的 while 循环中间。
MySQL 正在向您返回结果,一次一个结果行。因此,您应该做的是首先收集所有这些结果,然后再将它们打印出来(或者,或者对返回的行数进行计数器)。在第二段代码中,您将每个结果行视为第一段中的整个结果数组。
即 while($row = mysql_fetch_array($result))
行从表中返回一行。
因此,行 $id=$row['id'];
不将数组分配给 $id
.
因此,行 for ($p=0; $p<count($id); $p++) {
迭代单个项目,从而产生您所看到的结果。
我的代码看起来仍然有点hackish,但它可能会给你一个想法。恐怕我还没有测试过。
print "<table><tr>";
$p=0;
$columns=3;
while( $row = mysql_fetch_array($result) ) {
if ( $p>0 && ($p % $columns)==0 )
print "</tr><tr>";
print "<td>{$row['nam']}</td>";
$p++;
}
for(true;($p % $columns)!=0;$p++) //Finish off $p from above
print "<td> </td>";
print "</tr></table>";
以更加模块化的方式做到这一点:
function display($stuff,$cols){
//Make sure the table is some multiple of $cols to eliminate special cases
//Hackish
while( (count($stuff) % $cols)!=0 )
$stuff.push_back(" ");
//Start table and first row, eliminating another special case
print "<table><tr>";
for($i=0;$i<count($stuff);$i++){
if($i>0 && ($i % $cols)==0)
print "</tr><tr>";
print "<td>{$stuff[$i]}</td>";
}
print "</tr></table>";
}
$names=array()
while( $row=mysql_fetch_array($result) )
$names.push_back($row['nam']);
display($names,5);
关于php - mysql php 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7323624/