To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
This script prints data contained in 3 rows of 77 items each.
If anyone knows how to parse the file starting at row 3, item 64, taking only 12 items - I'd get the 12 items of data I need. Because the code above tells me what the last row is, it doesn't matter if there's 3 or 50 - I'll always start at the last row at the 64th item.
Any comments on the code above welcome too, I amended something I found on php.net.
It's not entirely clear to me what an "item" is in this case, nor exactly which rows/items you want, but you could load everything into an array, and then grab things from it as desired, using the various array functions to reverse it, sort it, grab chunks of it, etc.
PHP Code:
$data = array();
if(($fh = fopen('/path/to/file')) != false)
{
while($row = fgetcsv($fh, 1000) !== false)
{
if(count($row) == 1 and $row[0] === null)
{
continue; // skip empty rows
}
$data[] = $row;
}
}
// get 12 rows starting with 3rd row (offset 2):
$myData = array_slice($data, 2, 12);
// reverse the order of the selected rows:
$myData = array_reverse($myData);
foreach($myData as $row)
{
echo "The fifth element in this row is: " . $row[4] . "<br />";
}
The above is untested and merely intended to give you some ideas.
__________________
"That's what the gods are! An answer that will do! Because there's food to be caught and babies to be born and life to be lived and so there is not time for big, complicated, and worrying answers! Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." -- from Nation, by Terry Pratchett freelancer.internet.com Email me
Working through NogDog's example and referring to the manual I have code that does what I want (which has changed slightly from the initial 'last few items' question that the post began with but I feel the content is still on topic and will still assist others with CSV parse questions).
I'm parsing a csv file that contains varying numbers of rows; I want to get specific elements (is that the correct term? Fields?) from each row into different arrays.
The code below works fine but I can't see how it knows to read the next row in the CSV file.
I am incrementing $row but as it's not actually used in the WHILE command, how does it know to start the next one?
I'm obviously happy it works but I'd really like to know how! Can anyone assist please?
PHP Code:
$row = 1;
if (($handle = fopen('awreviewbox/be.csv', 'r')) !== FALSE) { // only proceed if the file is present
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { // only proceed if there is data in the csv
$num = count($data);
// There are $num of fields in row $row
$row++;
for ($c=0; $c < $num; $c++) {
// fill arrays with data
$name[$row-1]=$data[21];
$id[$row-1]=$data[22];
$reference[$row-1]=$data[24];
}
}
fclose($handle);
$totalrows = $row-1;
When reading from a file with a function such as fgetcsv() or fgets() PHP maintains a file pointer which points to the current position in that file. The pointer is automatically incremented each time a read is done, and can be directly manipulated with e.g. fseek() or rewind().
Reading ends when length - 1 bytes have been read, on a newline (which is included in the return value), or on EOF (whichever comes first). If no length is specified, it will keep reading from the stream until it reaches the end of the line.
The file pointer, btw, is bound to the resource returned by fopen().
My mistake; the quote is from the entry for fgets(), which is linked to from the entry for fgetcsv(). It applies to the length parameter for the former. That parameter for fgetcsv() works slightly differently.