2013-04-13 95 views
-1

我從html獲取名稱和數據。我想把它寫入CSV。Php fputcsv麻煩上校

所以我得到這個CSV。

ColA ColB   ColC ColD ColE ColF ColG 
name1 surname1  data1 data2 data3 data4 data5 
name2 surname2  data6 data7 data8 data9 data10 
name3 surname3  data11 data12 data13 data14 data15 
name..n surname..n data..n data..n data..n data..n data..n 

但我想在ColA的姓名。

 ColA   ColB ColC ColD ColE ColF 
name1 surname1  data1 data2 data3 data4 data5 
name2 surname2  data6 data7 data8 data9 data10 
name3 surname3  data11 data12 data13 data14 data15 
name..n surname..n data..n data..n data..n data..n data..n 

我認爲是因爲「空間」的名字和姓氏(有些是中間名),但數據沒有空間。我想在ColA中寫姓名。 Plzzz幫助我。 T形

$fp = fopen("data.csv","w"); 
    $string = ''; 
    for ($i=0; $i<$count_name; $i++) 
{ 
while ($iterate <= 5 and $j < $count_data) 
{ 
    $string = $string.$temp_data[$j]." "; 
    $j++; 
    $iterate++; 
} 
$csv_string = $temp_name[$i]." ".$string; 
//echo $temp_name[$i]; 
$array = explode(' ',$csv_string); 
//var_dump($array); 
fputcsv($fp,$array); 
//$string = ""; 
$iterate = 1; 
$string = ""; 

} 
+0

請儘量你想要什麼更加清晰。你想讀取和CSV並放入一個HTML,就是這樣? – vinigarcia87

+0

謝謝先生。我有一個好主意。 :) –

回答

0

如果我理解正確的,你應該試試這個:

$fp = fopen('data.csv', 'w'); 
for(/* Each line of data you have */){ 
    $colA = $name ." ".$surname; // Concatenate name and surname that you have received from your html 
    $colB = $data1; // Data1 from html 
    $colC = $data2; // Data2 from html 
    fputcsv($fp, array($colA,$colB,$colC)); // A new line on your csv 
} 
fclose($fp);