2011-02-07 54 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。

相關問題