Page 1 of 1

OK I'm sure this is easy

Posted: Mon Sep 22, 2003 5:27 pm
by wesnoel
How can I read the last line of a csv file using eof().

Something like

fopen("filename.csv");

echo eof;

fclose();

As you see I really have no clue of what I'm doing. :lol:

Please help me. :wink:

Posted: Mon Sep 22, 2003 5:45 pm
by volka
if the file's not too big you might read in the whole thing as an array and use the last element of that array

Code: Select all

<?php
$content = file('filename.csv');
echo $content[count($content)-1];
?>
or take a look at the first example at http://php.net/manual/en/function.fgetcsv.php
modified to your problem:

Code: Select all

<?php
$handle = fopen ('filename.csv','r');
while ($dataCurrent = fgetcsv ($handle, 1000, ","))
	$dataLast = $dataCurrent;
fclose ($handle);

print_r($dataLast);
?>

Posted: Mon Sep 22, 2003 6:45 pm
by wesnoel
Great Thanks that is awsome.

On a side note. What I am needing to do is list a bunch of csv files in a directory and next to each show the last value in the file. It will say "pending" or "N/A"

How could I loop though each?

I already have the script written to show and link to the csv files in the directory.

Code: Select all

<?php
$del = $_GET['del'];
if(isset($del)){
unlink("$del");
}


$current_dir = "files";
$heading ="<h2>Current Blue Slips</h>";

//echo $heading ."<br>" ;
echo "<br><table width="60%" border="0" cellspacing="0" cellpadding="1">";

$dir = opendir($current_dir);
while($file = readdir($dir))
    {
  if(($file=='.')||($file=='..')) continue;

 //WRITE THE CSV LISTING
 
 
 
echo "<tr><td><font color="#FF3300"size="1" face="Verdana">$status</font></td><td><font size="2" face="Verdana">$file</font></td><td><font size="2" face="Verdana"><a href="javascript:popUp2('view.php?note=files/$file')">View</a></font></td><td><font size="1" face="Verdana"><a href="javascript:popUp('files/$file')">EDIT</a></font></td><td><font size="1" face="Verdana"><a href="list.php?del=files/$file" target="_self">DELETE</a><td></td></font></td></tr>";

}
closedir($dir);

?>
All of the above code works great I just want to add that small bit of code.

Thanks

Wes/

Posted: Mon Sep 22, 2003 7:21 pm
by volka
e.g.

Code: Select all

<?php
$del = $_GET['del'];
if(isset($del)){
unlink("$del");
}


$current_dir = "files";
$heading ="<h2>Current Blue Slips</h>";

//echo $heading ."<br>" ;
echo "<br><table width="60%" border="0" cellspacing="0" cellpadding="1">";

$dir = opendir($current_dir);
$current_dir .= '/':
while($file = readdir($dir))
{
  if(($file=='.')||($file=='..')) continue;

	$content = @file($current_dir.$file);
	if ($content)
		$content = $content[count($content)-1]; 
	else
		$content =  '<unknown>';
?>
<tr>
	<td>
		<font color="#FF3300" size="1" face="Verdana"><?php echo $status; ?></font>
	</td>
	<td>
		<font size="2" face="Verdana"><?php echo $file; ?></font>
	</td>
	<td>
		<pre><?php echo $content; ?></pre>
	</td>
	<td>
		<font size="2" face="Verdana">
			<a href="javascript:popUp2('view.php?note=files/<?php echo $file; ?>')">View</a>
		</font>
	</td>
	<td>
		<font size="1" face="Verdana">
			<a href="javascript:popUp('files/<?php echo $file; ?>')">EDIT</a>
		</font>
	</td>
	<td>
		<font size="1" face="Verdana">
			<a href="list.php?del=files/<?php echo $file; ?>" target="_self">DELETE</a>
		</font>
	<td>
</tr>
<?php
}
closedir($dir);

?>
(tested not even by compiler)