Click to See Complete Forum and Search --> : Reading from a file


Gameline146
04-22-2004, 11:51 AM
I have a file that has list that look like this format on down the line for however many I have and this will change every week at least:

April 22, 2004 Blurb
link to blurb
April 22, 2004 Blurb2
link to blurb2

REALLY I would much rather have the text file have this form and be able to make the php parse it correctly:

April 22, 2004 Blurb link to blurb
April 22, 2004 Blurb2 link to blurb2



I want some php to open the text file and print html into a table like this for all the entries in the text file:

<tr align="center"><td><a href="link to blurb">April 22, 2004 blurb</a></td></tr>


I have use php for forms and connecting to databases but file I/O is alluding me. Thanks for the help!

Jona
04-22-2004, 11:57 AM
I use a similar method in my LinkDex program. The folder contains a file named "links.txt" You can customize it to your liking, as long as you change the corresponding variables. The file-format looks like this:


Link Name,http://sitename.com/
Link 2,http://site2.com/


And the code looks like this.


<ul>
<?php
$dir = getcwd(); // current directory
$linksBy = "\n"; // new entries on new lines
$descBy = ","; // put into format name,url
$desc = array();
$filetype = ".txt"; // file extension
$filename = "links"; // filename
$alpha = true; /* sort alphabetically, if set to false, it will sort by date */

$handle = @fopen($filename. $filetype, "r");
$data = fread($handle, filesize($filename . $filetype));
fclose($handle);

$links = explode($linksBy, $data);

if($alpha==true){
sort($links);
}

$linkLen = count($links);

for($j=0; $j<$linkLen; $j++)
{

$desc[] = explode($descBy, $links[$j]);

}
sort($desc);

for($j=0; $j<$linkLen; $j++)
{
echo("<li><a href=\"". $desc[$j][1] ."\" title=\"". $desc[$j][0] ."\">". $desc[$j][0] ."</a></li>\n");
}
?>
</ul>