|
|
|
|
![]() ![]() |
Feb 7 2006, 01:55 AM
Post
#1
|
|
|
A computer once beat me at chess, but it was no match for me at kick boxing. ![]() Group: [MODERATOR] Posts: 3,994 Joined: 24-July 05 From: In Trouble Again... still? Member No.: 9,787 ![]() |
Another Unordered List Script
If you remember this Script, that tutorial was about creating an un-ordered list of links from a list of files contained in a Folder which was specifically named in the script. This current Tutorial goes several steps beyond that first Tutorial by being able to create a list from several folder names and, more importantly, the script reads the list of links from a 'flat file' rather than depending on the name of the folder to be hard coded in the script. It is only one small step to have the folder names found in a mysql table, but I'll leave that aspect up to someone else. A sample is here This script reads a list of folder names from a text file and then builds a list of links from the filenames. The script uses the contents of the first <h3> tag as the link description and the file-name as the anchor href. The file should be an html file. This script may come in handy for creating a list of clickable links for use in sidebars and on 'sitemap' pages. Simply store html files in folders grouped by topic, ensuring that the description inside the first h3 html tag of each file is suitable for using as the display field for the anchor list. The Folder name displays as a heading for the block of links. This script is freely available for use by all who receive it. Sample page using this script CODE <?php print '<div id="sidebar">'; $folder_file ='flat_file_data.txt'; // name of flat file to read $fp = fopen($folder_file,'r'); // open flat file for reading if (!$fp) {print 'ERROR: Unable to open file'; echo '<br />';echo $folder_file;echo '<br />';exit;}// error if no handle found for flat file $line= fread($fp,1024); // increase this length if required $f_array=explode("\n",$line); $folder_key=0; //<!-- while loop start --> while ($f_array[$folder_key]) {; $fileCount = 0; $p_folder=strip_tags(nl2br($f_array[$folder_key])); // for security // print to begin un-ordered list using folder name as heading and as div id print '<div id="'.$p_folder.'">'; print '<b>'.$p_folder.'</b>'; $folder = $p_folder.'/'; if ($handle = opendir($folder)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $filename =$_SERVER['DOC_ROOT'].$folder.$file; $fp = fopen($filename, "r"); $contents = fread($fp, filesize($filename)); $pos1 = strpos($contents,"<h3>"); $pos2 = strpos($contents,"</h3>"); $str_length = $pos2 - $pos1; $title = substr($contents,$pos1+4,$str_length-4); $title_array[] = $title; fclose($fp); $file_array[]= $file; $fileCount++; } } } closedir($handle); if ($fileCount > 0) { $iterator = 0; while($title_array[$iterator]) { $file = substr($file_array[$iterator], 0, (strlen($file_array[$iterator]))); print '<li><a href="'.$folder.$file.'">'.$title_array[$iterator].'</a></li>'; $iterator ++; } unset($file_array); unset($title_array); } print '</div>'; $folder_key++; } //-- end while loop --> //fclose($fp); // close flat file print '</div>'; ?> Sample File Contents: HTML <head></head> <title></title> <body> <h3>An article name</h3> </body> Sample Flat File Contents: QUOTE artfold artfold1 artfold2 ... artfoldn Enjoy. |
|
|
|
Feb 7 2006, 06:51 AM
Post
#2
|
|
|
apt-get moo ![]() Group: [MODERATOR] Posts: 2,114 Joined: 28-May 05 From: Hertfordshire, England Member No.: 7,593 ![]() |
Excellent script. I was going to write something like this myself for a website, but you've already done it, thanks
|
|
|
|
Feb 7 2006, 01:10 PM
Post
#3
|
|
|
Privileged Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 570 Joined: 5-July 05 From: Ballarat Member No.: 9,042 |
You've Done it again jlhaslip, nice and simple. I've been looking for something like that, and you played it right into my hands. Thanks!...
|
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 6th September 2008 - 07:52 PM |