Jump to content

automaticly displaying the most recent file in a blog style page


Recommended Posts

Ok.. what i'm trying to accomplish here seems really simple, but i have absolutly no idea how to proceed (or just how [i]simple[/i] 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 [b]name[/b] 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 [i]that[/i] 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?
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 [i]something[/i] like this:
[code]<?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;
?>[/code]
If you want to go back a file, increment $k - myscript.php?k=1 - Would go back a file.
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. 
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?
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
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.