|
|
|
|
![]() ![]() |
Apr 16 2007, 08:42 AM
Post
#1
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 413 Joined: 4-October 06 From: Psychedelic Realms Member No.: 31,079 |
Some user posted a similar problem i had when i tried to figure out how to update content on my website in less work as possible. This is just part of the "big" plan i have for my site but it can be helpfull to you guys if you like FlatFile. I hope that mod's don't mind me posting the same code here and on their forums, couse if they do i'll delete it from their forums
So here's the situation. Let's say you have a folder called 'myfilesdirectory' on your server, in which you upload your text files which you want to use on your website. Also you want those files to be somehow accesible via hyperlinks. Smartest way to do so, with as less as work as possible is to use opendir(), readdir(); and some basic php functions like i'll explain now. My folder contains of these *.txt files, news.txt, contact.txt, links.txt. I want to read those files to an array, which i will use later for two things. First to dynamicly create hyperlinks of those files, and second to asign $_GET values to them, so that we don't need to get away from index.php page at any time. So here's the full code CODE <?php $directory = getcwd(); $files=array(); #initialize array $dir = opendir("$directory/myfilesdirectory"); while (false !== ($f = readdir($dir))) { if (eregi("\.txt",$f)){ #if filename matches .txt in the name $fstristr = substr($f, 0, -4); array_push($files,"$fstristr"); #push into $files array } } foreach ($files as $link){ echo '<a href="?id='.$link.'">'.ucfirst($link).'</a> '; } echo "<br>"; if(in_array($_GET['id'], $files)){ include_once $directory.'/myfilesdirectory/'.$_GET['id'].'.txt'; } else { include_once $directory.'/myfilesdirectory/news.txt'; } ?> Now to explain it piece by piece... 1) CODE $directory= getcwd(); set our directory to the position of our script. There are other ways to do it, but i like this one.2) CODE $files = array(); initialize $files array which is now empty, but we will fill it later with some PHP functions3) CODE $dir = opendir("$directory/myfilesdirectory"); sets variable $dir to open 'myfilesdirectory' in which i saved my files4) CODE while (false !== ($f = readdir($dir))) is used to QUOTE Returns the filename of the next file from the directory. The filenames are returned in the order in which they are stored by the filesystem. 5) CODE if (eregi("\.txt",$f)){ #if filename matches .txt in the name $fstristr = substr($f, 0, -4); array_push($files,"$fstristr"); #push into $files array } CODE eregi() is used so that only files with .txt extensions get $f valueCODE $fstristr = substr($f, 0, -4) is used to strip those extensions by using substr() to read file until last 4 characters which are .txt CODE array_push($files, "$fstristr") now we use array_push to fill that $files array with our files that are read and striped nicely to fit our needs. At this time $files array looks something like thisarray ([0] => "content", [1] => "links", [2] => "news") notice how it sorted out array, maybe later i will use some kind of function to sort it more convinient than it is now Basicly that is the "new" code, the rest of the code is more or less familiar to even newbies in php, but i'll explain it briefly.. 6) CODE foreach ($files as $link){ foreach is used to dynamicly create links from the $files array (ucfirst is used to capitalize first letters)echo '<a href="?id='.$link.'">'.ucfirst($link).'</a> '; } and last but not the least 7) CODE if(in_array($_GET['id'], $files)){ include_once $directory.'/myfilesdirectory/'.$_GET['id'].'.txt'; } else { include_once $directory.'/myfilesdirectory/news.txt'; } ?> This script looks weather a $_GET['id'] is in array $files, and if true includes that file. Else is the default file which is going to be loaded while $_GET is not yet set. The preview of the script is availiable here And a download is availiable here This post has been edited by matak: May 13 2007, 11:29 PM |
|
|
|
Apr 16 2007, 07:43 PM
Post
#2
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 299 Joined: 27-January 07 From: Winter is cold here. Member No.: 37,984 ![]() |
Thanks, dude! Like your new avatar, too! You last few php topic have been other ways of using the include() function to get the ?id=links pages. Some creative ways to get around the include() function using arrays! I might use this on my site soon! Thanks!
|
|
|
|
Apr 17 2007, 06:35 PM
Post
#3
|
|
|
Advanced Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 144 Joined: 22-March 07 Member No.: 40,472 |
Greetins
thanx mate for the nice tutorial it explains me a lot here lokking forward to see more of you tutorialss Bye |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 11th October 2008 - 04:35 AM |