2011-02-07 103 views
0

我从数据库中提取数据并假设表中有23列,那么我必须编写23行来设置值。 有没有其他简短的方法来做到这一点?在php中执行此操作的最佳方法

在下面的例子中,只有15列,我写了15行。任何短路?

while($row = mysql_fetch_array($export)) 
    { 
     $line = ''; 
     $line.=setValue($row[0]); 
     $line.=setValue($row[1]); 
     $line.=setValue($row[2]); 
     $line.=setValue($row[3]); 
     $line.=setValue($row[4]); 
     $line.=setValue($row[5]); 
     $line.=setValue($row[6]); 
     $line.=setValue($row[7]); 
     $line.=setValue($row[8]); 
     $line.=setValue($row[9]); 
     $line.=setValue($row[10]); 
     $line.=setValue($row[11]); 
     $line.=setValue($row[12]); 
     $line.=setValue($row[13]); 
     $line.=setValue($row[14]); 

    } 

回答

6
while($row = mysql_fetch_array($export)) 
{ 
    $line = ''; 
    foreach($row as $value) { 
     $line.=setValue($value); 
    } 
} 
0

我真的不知道,如果这是你在找什么,但你可以使用一个for循环是这样的:

while($row = mysql_fetch_array($export)) 
    { 
     $line = ''; 
     for($i = 0; $i < 15; $i++) 
     { 
      $line.=setValue($row[$i]); 
     } 

    } 
0
while($row = mysql_fetch_array($export)) 
    { 
     $line = ''; 
     for($i = 0; $i < 15; $i++) { 
      $line .= setValue($row[$i]); 
     } 

    } 

这是一种方法。还有其他人。您可以用返回数组的计数来替换'15'。这样,for循环将迭代直到数组结束。

UPS,哈哈,来不及了......

+0

在循环上使用固定边界是凌乱的 - 评估每次迭代的计数是不合适的 – symcbean 2011-02-07 12:22:13

0

虽然你可以做这样的事情....

while ($row=mysql_fetch_array($export)) { 
    $line=array(); 
    $line=array_map('setValue',$xport); 
    $line=implode('',$line); 
} 

....它的一个非常混乱的结构 - 它的前提是,查询将返回一个非常好定义的结果集,并且结果集及其后续应用程序的结构将被删除;作为一般的经验法则,你不应该使用mysql_fetch_array - 而是使用mysql_fetch_assoc。

相关问题