Jump to content

Noob needs some help with includes


Wheatley

Recommended Posts

Ok, I am sure this is really simple for all of you, but I have very little experience with php. I haven't sat down to learn php yet, but I will when I have more free time. In the meantime I have the following issue:

 

The site is http://astoryaday.fromyay.com/

 

I have all the stories in the /stories/ directory, all named 20110504.html, 201105005.html, etc. Every day I write another story, I name it following that pattern, and drop it into the /stories/ folder.

 

I only want the newest story displayed on the page. So I wrote a little script that is:

<?php
$files = glob("stories/*.html");
sort($files);
$newest = array_pop($files);
include($newest);
?>

 

Now, what I want to do, is in the footer, put an Archive that will look like this:

 

-2012

--January

---03

---02

---01

 

-2011

--December

---31

---30

 

And upon clicking on the dates, the story from that day will replace the story that is currently included.

 

Any help of how to get this done would be appreciated! :D

Thanks

Link to comment
Share on other sites

I would use a database to store the content of each story and just use a $_GET parameter on the end of the URL to determine which store to retrieve the content for and output it on one single page (i.e. a simple content management system.) This will eliminate the need to make a copy of each page (or of your master page) and edit it.

 

You would make the links to the archive by retrieving a list of all the available stories (either from a database or by using the glob() function on your files.) You would then loop through the list of the available stories and get the year, month, and day information from each one. When the year value changes, you would output a new year heading. When the month value changes, you would output a new month heading. You would then output each day as a <a href=""></a> link. The links would contain an id as a get parameter on the end of the url that indicates which story to retrieve (database) or include (file) into the page.

Link to comment
Share on other sites

I would use a database to store the content of each story and just use a $_GET parameter on the end of the URL to determine which store to retrieve the content for and output it on one single page (i.e. a simple content management system.) This will eliminate the need to make a copy of each page (or of your master page) and edit it.

 

You would make the links to the archive by retrieving a list of all the available stories (either from a database or by using the glob() function on your files.) You would then loop through the list of the available stories and get the year, month, and day information from each one. When the year value changes, you would output a new year heading. When the month value changes, you would output a new month heading. You would then output each day as a <a href=""></a> link. The links would contain an id as a get parameter on the end of the url that indicates which story to retrieve (database) or include (file) into the page.

 

Yeah I was trying to avoid that, because it seems more complicated than it should be.. plus I have no idea how to do it  :(

Link to comment
Share on other sites

<?php
$path = "stories";
$story = isset($_GET['story']) ? basename($_GET['story']) : ''; // condition input
$files = glob("$path/*.html"); // list of all files
sort($files);
if(empty($story)){
// get the lastest one
$newest = end($files);
echo "Story from: $newest<br />\n";
include($newest);
} else {
// get the story based on $story
// $story is the basename of value supplied on the end of the url
$file = "$path/$story";
if(file_exists($file)){
	// requested file exists in the correct folder
	echo "Story from: $file<br />\n";
	include($file);
} else {
	// file does not exist
	echo "The requested story for: $story, does not exist!";
}
}

// archive links
$last_yr = ''; // remember the last value
$last_mo = ''; // remember the last value
if(is_array($files) && !empty($files)){
foreach($files as $file){
	// path/yyyymmdd.html
	$name = basename($file,'.html');
	preg_match("/(\d{4})(\d{2})(\d{2})/", $name, $matches);
	// $matches[1], [2], [3] = year, month, day
	if($last_yr != $matches[1]){
		// new year heading
		echo $matches[1] . "<br />\n";
		$last_yr = $matches[1]; // save the new value
	}
	if($last_mo != $matches[2]){
		// new month heading
		$mn = date('F',mktime(0,0,0,$matches[2],1,1970));
		echo $mn . "<br />\n";
		$last_mo = $matches[2]; // save the new value
	}
	// output the data
	echo "<a href='?story=$name.html'>$matches[3]</a><br />\n";
}
} else {
echo "There are no stories available!";
}
?>

Link to comment
Share on other sites

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.