Alright, so I'm making a blog that uses a flat file instead of a database. I made a JavaScript form to submit the information to a PHP file that writes it to a text file. That text file is then displayed by a PHP file, which I have framed in an HTML file. Short and simple, eh? Anyway, currently posts write to the end of the text file, causing them to be in the wrong order when the blog is displayed. How do I change the following code so that the posts are added to the beginning of the file, not the end? sendinfo.php (The file that sends the form data to the text file): <html> <head> <title>Posting</title> </head> <body> <?php $date = $_GET["date"]; $title = $_GET["title"]; $message = $_GET["message"]; print("Post submitted."); $out = fopen("blogfile.txt", "a"); if (!$out) { print("Could not append to file"); exit; } fputs ($out,implode,("\n")); fwrite($out,"$date|$title|$message"); fwrite($out,("\n")); fclose($out); ?> </body> </html> PHP:
Appending the most recent entries to the end of the file makes sense. Maybe you should change the code that shows the post, to have them sorted accordingly? Maybe something like this untested code: <?php function get_posts_descending($file, $limit=1) { $limit = is_int($limit) ? $limit : 1; $lines = array(); $linecontent = null; $contents = file($file); $loopCounter = 0; foreach ($i=count($contents)-1; $i>=0; $i--) { $linecontent = $contents[$i]; // do something wtih $linecontent // put the result at the end of lines $lines[] = // whatever the result is if( ++$loopCounter >= $limit ) break; } return $lines; } ?> PHP:
This is my first ever PHP project, so could you please show me what you mean? Thanks so much for the help! Blog file: <? echo " <style type='text/css'> .fixedfont { font-size : 9pt; font-family : Arial, Helvetica, sans-serif; color : #333333 } </style> <div class='fixedfont'> "; // Determines File you want to use $opFile = "blogfile.txt"; // Opens Blog File to read or dies $fp = fopen($opFile,"r") or die("Error Reading File"); $data = fread($fp, filesize($opFile)); fclose($fp); // Explodes data at line breaks $line = explode("\n", $data); $i=count($line); for ($n=0 ; $n < $i-1 ; $n++ ) { $blog = explode("|", $line[$n]); if (isset($blog[0])) { echo "<strong>" .$blog[0]."</strong> - "; echo "<strong>" .$blog[1]."</strong><br><br>"; echo "".$blog[2]."<br><br>"; } } echo "</div>"; ?> PHP:
<?php echo " <style type='text/css'> .fixedfont { font-size : 9pt; font-family : Arial, Helvetica, sans-serif; color : #333333 } </style> "; // Determines File you want to use $opFile = "./blogfile.txt"; $maxNumPostsToGet = 5; $lines = get_posts_descending($opFile, $maxNumPostsToGet); if (!empty($lines)) { echo "<div class='fixedfont'>"; foreach ($lines as $line) { $blog = explode('|', $line); if (isset($blog[0])) { echo "<strong>" .$blog[0]."</strong> - "; echo "<strong>" .$blog[1]."</strong><br><br>"; echo "".$blog[2]."<br><br>"; } } echo "</div>"; } function get_posts_descending($file, $limit=1) { if (!strlen($file) || !file_exists($file)) { // This should be caught with a custom error handler // http://us3.php.net/manual/en/function.set-error-handler.php trigger_error("Must provide valid file path as first parameter in function get_posts_descending()", E_USER_ERROR); return; } $limit = is_int($limit) ? $limit : 1; $lines = array(); $contents = file($file); $loopCounter = 0; for ($i=count($contents)-1; $i>=0; $i--) { $lines[] = $contents[$i]; if( ++$loopCounter >= $limit ) break; } return $lines; } ?> PHP: I haven't tested this, but you can try it and see if it works, if it doesn't, post your results. If it _does_ work, you should want to know why.
Thanks, but I found another way. Thought you'd given up so I went ahead and made this thread: http://forums.digitalpoint.com/showthread.php?t=157960. If you wanna check out the blog though, you can at www.monochromedia.com/weblog.html. Again, thanks anyway, and sorry for the trouble.