Jump to content

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/139562-php-blog-simpe-problem-help-needed/
Share on other sites

<?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.

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.