Wheatley Posted May 5, 2011 Share Posted May 5, 2011 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! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/235635-noob-needs-some-help-with-includes/ Share on other sites More sharing options...
The Little Guy Posted May 5, 2011 Share Posted May 5, 2011 I wouldn't do an include, I would do file functions $file = 'stories/20110504.html'; $handle = fopen($file, 'rb'); $content = fread($handle, filesize($handle)); fclose($handle); echo $content; Quote Link to comment https://forums.phpfreaks.com/topic/235635-noob-needs-some-help-with-includes/#findComment-1211115 Share on other sites More sharing options...
PFMaBiSmAd Posted May 5, 2011 Share Posted May 5, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235635-noob-needs-some-help-with-includes/#findComment-1211116 Share on other sites More sharing options...
Wheatley Posted May 5, 2011 Author Share Posted May 5, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/235635-noob-needs-some-help-with-includes/#findComment-1211119 Share on other sites More sharing options...
PFMaBiSmAd Posted May 5, 2011 Share Posted May 5, 2011 <?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!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/235635-noob-needs-some-help-with-includes/#findComment-1211154 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.