Forum Moderators: coopster
First post so hello, this is probably a simple question but as I know very little about PHP it's proving difficult for me, I have the below script & all I want to do i be able to skip the fist 3 rows of the csv file it reads, possible?
<style type="text/css">
<!--
.row1 { background: #eed; }
.row0 { background: #fff; }
-->
</style><?php
$current_row = 0; //alternate row highlighting
$filename = "myfile.csv"; //here's the filename
$id = fopen($filename, "r"); //open the file
while ($data = fgetcsv($id, filesize($filename))) //start a loop
$table[] = $data; //put each line into its own entry in the $table array
fclose($id); //close file
echo "<table>\n";
foreach($table as $row)
{
$current_row = 1 - $current_row;
echo "<tr>";
foreach($row as $data)
echo "<td class=row".$current_row.">$data</td>";
echo "</tr>\n";
}
echo "</table>\n";
?>
Thanks
David
/*
Opens file
Assigns resource to $handle
*/
$handle = fopen($myfile, "r");
/*
goes through first 3 lines
you may want incorporate this in the while loop below for a better look
*/
$line = fgets($handle);
$line = fgets($handle);
$line = fgets($handle);
/*
loops for each line until end of file(feof)
*/
while (!feof($handle)){
$line = fgets($handle);
}//ends while
see links below for more information
[us.php.net...]
[us.php.net...]
foreach($table as $row)
{
$current_row = 1 - $current_row;
echo "<tr>";
foreach($row as $data)
echo "<td class=row".$current_row.">$data</td>";echo "</tr>\n";
}
To:
foreach($table as $n=>$row)
{
if ($n>2)
{
$current_row = 1 - $current_row;
echo "<tr>";
foreach($row as $data)
echo "<td class=row".$current_row.">$data</td>";echo "</tr>\n";
}
}
Basically, we get those array keys (0, 1, 2 ...) assigned as $n from your foreach loop, and then we edit the loop internals so that we don't do anything for keys under 3.