Jump to content


Photo

automaticly displaying the most recent file in a blog style page


  • Please log in to reply
6 replies to this topic

#1 iMeow

iMeow
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 12 July 2006 - 01:05 AM

Ok.. what i'm trying to accomplish here seems really simple, but i have absolutly no idea how to proceed (or just how simple it really is..)

building a site which will be seeing many additions, blog style, which will be added inside a pre-determined folder. the file names contain all the info i need (date, name). what i'd like to do is have the home page open that folder, find the most recent file (by looking at their names, which as i've mentioned earlier will contain the date) and display that file by default, in the home page. possibly using the include() command.  if one wishes to go backwards, a "back" button would be available, then the code would look for the 2nd most recent file. pressing the "back" button again would take the user to the 3rd most recent file, etc. on top of that, i'd like the <title> of my page to reflect the name of the currently displayed file, omiting the date.

ex: 2006.07.11.this is the title.txt

the code would see this as being the most recent file and include that by default in the home page. if tomorow i were to upload 2006.07.12.this is a new file.txt, then the home page would display that file by default, and to view the 11th of june file you'd simply have to press the "back" button.

basicly, making constant updates without ever having to touch the code.

possible? or pipe dreams?

#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 12 July 2006 - 12:29 PM

Very possible, although I personally would have taken a completely different path and gone with a database driven solution... But onto your method, you could do something like this:
<?php
$k = (isset($_GET['k'])) ? $_GET['k'] : 0; //Get they array key from the URL
$files = glob("path/to/your/files/*.txt"); //Get the list of files from your directory, they should be auto-sorted by default.
$content = file_get_contents($files[$k]); //Get the content of the correct file in the $files array
echo $content;
?>
If you want to go back a file, increment $k - myscript.php?k=1 - Would go back a file.

#3 iMeow

iMeow
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 12 July 2006 - 04:44 PM

thanks a lot, i'll give that a try when i get home. you said in your post that you would have gone the database way.. any particular advantages over my method i should be aware of, or just plain personal preference?

#4 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 12 July 2006 - 04:50 PM

Databases are much more efficient and faster than text files.  Databases are also much more easily managed from a search/ordering manner.  There is no SQL-like syntax for text files... you end up playing with arrays and doing all kinds of sorts and such to get orders right. 

Info: PHP Manual


#5 iMeow

iMeow
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 12 July 2006 - 05:01 PM

i see, what i liked about my method however is it seems simple in my head  ;D and the .txt was simply an example, i plan on using this template for many more types of files, such as images and all.. still possible using databases?

#6 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 12 July 2006 - 05:05 PM

Welll yes, although the image itself isn't in the database, you can contain all the information about it and its url.

#7 iMeow

iMeow
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 12 July 2006 - 09:28 PM

well i've researched SQL a bit, and it does seem very interesting and would definitly be a good way to go, but my only issue is with.. how "exacly" does databases work? i understand so far that i need to install mysql or similar program to create and edit databases, but beyond that, i mean does it save a file calls soandso.sql in my domain's root? say for instance i deal with webhostcompanyA, then decide that they simply do not cut it for me anymore and want to migrate my page to webhostcompanyB, it would be nice to simply pick up my file and throw em at their new home without ever touching anything of the code, but how will i bring the database over to webhostB? i plan on backing up my files on my computer, but how would i go about backing up a database? not that i dont trust webhost companies or anything *cough* but accidents happen, drives crash and i like to be prepared.. so far none of the sites i've visited address this at all.

but barring that i'm ready to look into using sql as it sounds perfect for what i'm trying to do




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users