Forum Moderators: coopster

Message Too Old, No Replies

Skip x Number of Rows

         

dholmes

1:54 pm on Sep 25, 2007 (gmt 0)

10+ Year Member



Hi,

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

d40sithui

2:33 pm on Sep 25, 2007 (gmt 0)

10+ Year Member



Hi David,
This might head u in the right direction.

/*
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...]

dholmes

3:16 pm on Sep 25, 2007 (gmt 0)

10+ Year Member



Thanks for your reply d40sithui, bit over my heed i'm afraid!

vincevincevince

3:29 pm on Sep 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Change from:
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.

dholmes

3:33 pm on Sep 25, 2007 (gmt 0)

10+ Year Member



Many thanks, works a treat