Thank you in advance for your time/help. I'm a newbie learning php, so please keep that in mind.
1st Question I need a php script that reads a csv file.
2nd Question How do I echo a specific cell (row and row/column) in that file
I found this script on a similar reply, the script reads the whole file perfectly, but I only want to select a specific cell/value on that csv file. So how do I echo a specific row or a row and column.
$row = 1;
if (($handle = fopen("1.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n"开发者_运维百科;
}
}
fclose($handle);
}
If you need to select several cases, the best way is to make a multi-dimensional array in php. Otherwise, just throw a few counters into your while loops and done.
$row = 1;
$mycsvfile = array(); //define the main array.
if (($handle = fopen("1.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
$mycsvfile[] = $data; //add the row to the main array.
}
fclose($handle);
}
echo $mycsvfile[3][1]; //prints the 4th row, second column.
We can create a table and table fields according to the csv header with the below script. Instead of printing the header, I want to create a table with the table fields as the header. The data in each field may be varchar(50)
$row = 1;
if (($handle = fopen("1.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
精彩评论