|
Posted by Jenny Purcell on 11/01/07 09:45
Below is a subroutine built from code I found at PHPFreaks, and it
works just as I'd expect it to. It lists all the files in a directory
and creates links to them.
I use it in a directory where each page is named for an animal, and
the page profiles that pet. What I'd like to be able to do is create a
separate text file in the same directory, with bits of information
about each pet, then, have that information added on the same line as
their link page.
So, right now the list looks something like this:
o fido.txt
o mittens.txt
o slither.txt
What I'd like to be able to do is have a text file with lines this:
slither.txt - my brand new Boa
fido.txt - a 12 year-old English Bulldog.
mittens.txt - my son's cute and fluffy kitten
sylvester.txt - a black and white cat given to my cousin
Then, when it displays on the page, I'd like it to display like this:
o fido.txt - a 12 year-old English Bulldog.
o mittens.txt - my son's cute and fluffy kitten
o slither.txt - my brand new Boa
The two elements that are important (other than getting the info line
at all) is that the list in the text file can be in any order, and
that entries could be in there that don't have link pages and they
shouldn't be displayed.
<?php
// Links creating script taken from:
http://www.phpfreaks.com/tutorials/146/0.php
// Originally added October 21st, 2007
// Modified by Jenny Purcell to be a subroutine to call it multiple
times.
function make_links($dir) { // create a user-defined function named
make_links
$files = scandir($dir);
foreach($files as $ind_file){ // start foreach loop
$letter = strtoupper(substr($ind_file,0,1));
if($ind_file != '.' && $ind_file != '..') { // start
if loop
?>
<li><a name="<?php echo $letter; ?>"
href="<?php echo "$dir"."/".$ind_file; ?>">
<?php echo $ind_file;?></a></li>
<?php
} // end if loop
} // end foreach loop
} // end user-defined make_links function
?>
Navigation:
[Reply to this message]
|