2011-09-06 56 views
0
<?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>&nbsp;</td>"; 
         } 
       print "</tr></table>"; 
     } 
} 
?> 

结果:MySQL的PHP​​列

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>&nbsp;</td>"; 
         } 
       print "</tr></table>"; 
     } 
} 
} 

mysql_close($con); 
?> 

结果:

ann 
brenda 
charles 
david  
edward 
florence  
geoff  
harry  
ingrid 
james  
kelly  
liam 

问题:怎么了?

Dabase表

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  
+0

我需要得到结果和列。相同的结果 – Tomas

回答

1

我建议你拆你的代码为两个不同的功能。

一个函数将读取数据库或数组中的信息,另一个函数将格式化输出。

现在,它看起来非常像你把你的第一块代码放到第二块while循环的中间。

MySQL正在向您返回结果,一次返回一个结果行。所以你应该做的是首先收集所有这些结果,然后再打印出来(或者打回返回的行数)。在你的第二段代码中,你将每个结果行视为第一块结果的整个数组。

也就是说,行while($row = mysql_fetch_array($result))从表中返回一行。因此,行$id=$row['id'];不会将数组分配给$id

正因为如此,行for ($p=0; $p<count($id); $p++) {迭代了一个项目,导致你所看到的。

我的代码看起来仍然有点骇人,但它可能会给你一个想法。恐怕我没有测试过它。

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>&nbsp;</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("&nbsp;"); 

    //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); 
+0

不知道我是否问得太多,但我仍然对脚本不熟悉,很高兴看到它应该如何。 – Tomas

+0

如果你在与他们斗争之前,你会学到最好的东西,直到你尽可能多地想出来。也就是说,看好代码是编写优秀代码的先决条件。给我几分钟... – Richard

+0

好吧,不能承诺,这是很好的代码,但它可能适合你... – Richard