How can I have my html table-based-message-board display the newest messages first?

时间:2017-08-04 13:06:19

标签: php fopen

I made this website message board based in HTML and PHP. It's very annoying having to scroll down to the very bottom every time to view the newest message, so how can I make the newest appear at the top?

In the index.php, a user will put in a desired display name, and message. after they submit it, it will take them to code_exec.php, which sends the variables to the board.html.

This is code_exec.php

<?php
session_start();
$nick=htmlspecialchars($_POST['nick']);
$text=htmlspecialchars($_POST['text']);
$dateTime = date('d/m/y G:i:s');

$data = "<tr>
<td>   $dateTime  </td>
<td>   $nick   </td>
<td>   $text   </td>
</tr>";

$fh = fopen('board.html', 'a');
fwrite($fh, $data);
fclose($fh);

<script type="text/javascript">
window.location = "board.html"
</script>';

On the board.html it ends up looking like this

enter image description here

Any help appreciated!

1 个答案:

答案 0 :(得分:0)

instead of using fopen use file_put_contents and

$file_data = $data."\n";
$file_data .= file_get_contents('board.html');
file_put_contents('board.html', $file_data);