OK I'm sure this is easy

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wesnoel
Forum Commoner
Posts: 58
Joined: Fri Sep 05, 2003 11:53 am

OK I'm sure this is easy

Post 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:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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);
?>
wesnoel
Forum Commoner
Posts: 58
Joined: Fri Sep 05, 2003 11:53 am

Post 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/
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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)
Post Reply