danielandlisa Posted January 5, 2009 Share Posted January 5, 2009 Hi guys, im currently learning php and have managed to make php flatfile blog, i only got one problem that i like to show the newest message on top of the page right now the new messages display at the bottom. Iknow mysql is ab tter solution for php but this have to be done so i hope you guys can help me. I display the code below. I guess the code for displaying the newest message first should be in post.php? blog.php <? echo "<font face=verdana >"; $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 "name: " .$blog[0]."<br>"; echo " date: " .$blog[1]."<br>"; echo " subject: " .$blog[2]."<br>"; echo " message: " .$blog[3]."<br><br>"; } } ?> post.php the file i use the form to post to <?php $filename = "blogfile.txt"; if (!isset($meddelande)) { $namn = $_POST['name']; //$date = $_POST['date']; $ämne = $_POST['subject']; $message = $_POST['message']; } $postdate = date('d M Y'); $blog = $name."|".$postdate."|".$subject."|".$message."|[end]\n" ; $data = fopen($filename, "a"); fwrite($data, $blog); fclose($data); echo "message sent"; ?> And ofcourse i got the post message and text file but i guess you dont need that. / lisa Quote Link to comment https://forums.phpfreaks.com/topic/139562-php-blog-simpe-problem-help-needed/ Share on other sites More sharing options...
premiso Posted January 5, 2009 Share Posted January 5, 2009 <?php echo "<font face=verdana >"; // put blog data in an array $line = file("blogfile.txt"); $i=count($line); $line = array_reverse($line); for ($n=0 ; $n < $i-1 ; $n++ ) { $blog = explode("|", $line[$n]); if (isset($blog[0])) { echo "namn : " .$blog[0]."<br>"; echo " datum: " .$blog[1]."<br>"; echo " ämne: " .$blog[2]."<br>"; echo " meddelande: " .$blog[3]."<br><br>"; } } ?> http://us.php.net/manual/en/function.array-reverse.php used file cause it is less code and more efficient, then used the above function to reverse the array so the newest message should display on top. Questions let me know. Quote Link to comment https://forums.phpfreaks.com/topic/139562-php-blog-simpe-problem-help-needed/#findComment-730072 Share on other sites More sharing options...
danielandlisa Posted January 5, 2009 Author Share Posted January 5, 2009 Thanks alot that was just what i needed. Im looking for somebody to help me with a few things on msn maybe you or somebody else is interesed? I will pay you for your time ofcourse paypal or something. Quote Link to comment https://forums.phpfreaks.com/topic/139562-php-blog-simpe-problem-help-needed/#findComment-730106 Share on other sites More sharing options...
danielandlisa Posted January 5, 2009 Author Share Posted January 5, 2009 i got one more problem when i use the form to send text to the datafile it dont work to make any linebraks it displays br on the blog site after using the form to send the data to to the text file. Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/139562-php-blog-simpe-problem-help-needed/#findComment-730250 Share on other sites More sharing options...
Maq Posted January 6, 2009 Share Posted January 6, 2009 .txt files use "/r/n" for line breaks while browsers use the HTML " " for line breaks. Quote Link to comment https://forums.phpfreaks.com/topic/139562-php-blog-simpe-problem-help-needed/#findComment-730576 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.