PHP Help - Write CSV files with PHP

//VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
// PHP Help - Write CSV files with PHP
//VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
Programmer : Mohd Izzairi Yamin

This sample file will read from a CSV file and create an SQL insert string to insert to a fictional database (no actual SQL INSERT is performed here - just an example of concept)

<?php
$row = 1;
$handle = fopen(”samplecsv.csv”, “r”);
while (($data = fgetcsv($handle, 1000, “|”)) !== FALSE) { // 2000x
$num = count($data);
echo “<p> $num fields in line $row: <br /></p>\n”;
$row++;
for ($c=0; $c < $num; $c++) { // 4x
echo $data[$c] . “<br />\n”;

}

$data[0] = $data[0] * .75;
$sql = “INSERT INTO DB
(col1,col2,col3,col4)
values
(’”.$data[0].”‘,’”.$data[1].”‘,’”.$data[2].”‘,’”.$data[3].”‘)”;
echo “<li>”.$sql;
}
fclose($handle);
?>

Sample CSV DATA
—————
“5″|”ALERT”|”0121111111″|”Hello”|
“5″|”ALERT”|”0121111111″|”Are”|
“5″|”ALERT”|”0121111111″|”You”|
“5″|”ALERT”|”0121111111″|”There”|
“5″|”ALERT”|”0121111111″|”Doing”|


About this entry