Jump to content
xisto Community
jlhaslip

Script To Build A List Of Links from filenames and h3 tags

Recommended Posts

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

 

<?phpprint '<div id="sidebar">'; $folder_file ='flat_file_data.txt';	 // name of flat file to read$fp = fopen($folder_file,'r');		// open flat file for readingif (!$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 idprint '<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 fileprint '</div>';  ?> 

Sample File Contents:

 

<head></head>
<title></title>
<body>
<h3>An article name</h3>
</body>

 

Sample Flat File Contents:

 

artfold

artfold1

artfold2

...

artfoldn

 

Enjoy.

Share this post


Link to post
Share on other sites

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!...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.