First: here is my script:
PHP Code:
<?php
// Configuration (change these if you want)
$filename = "shouts.txt"; // The file to store shouts in
$date = "jS M, g:i A"; // Date format for shouts (see http://php.net/date)
$timemod = 0; // Time modifier, in hours, use this to adjust the time
$amount = 0; // Amount of shouts to show (0 = all)
$shouthtml = <<<HTML
{date}
<br />
{message}
<p />
HTML;
$html = <<<HTML
{shouts}
<form method="post">
{errors}
<p />
Message: <input type="text" name="message" />
<p />
<input type="submit" name="submit" value="Shout" />
</form>
<p />
HTML;
// Code
if (!file_exists($filename))
{
// File does not exist, create it
$f = @fopen($filename, "w") or die("Cannot access $filename, wrong permissions maybe?");
fclose($f);
}
if (isset($_POST['submit']))
{
// User has clicked the "Shout" button
// Make safe all posted data, also store other data
if (get_magic_quotes_gpc())
{
$postmessage = trim(stripslashes(htmlspecialchars($_POST['message'])));
}
else
{
$postmessage = trim(htmlspecialchars($_POST['message']));
}
$postdate = time();
if ($postmessage == "")
{
$error .= "<span style=\"color: #FF0000;\">Please enter a message.</span> <br />";
}
if (isset($error))
{
// An error has occurred
// Format "error" part of HTML
$html = str_replace("{errors}", $error . "<p />", $html);
}
else
{
// No errors :)
// Add the shout
$f = fopen($filename, "a");
fwrite($f, "[Date] $postdate\n[Msg] $postmessage\n");
fclose($f);
header("Location: " . $_SERVER['PHP_SELF']);
die();
}
}
// Below: Handle all "shout" formatting
$i = 0;
$f = file($filename);
foreach ($f as $val)
{
// Loop through every line in $filename
if (preg_match("/\[Date\] [^A-Za-z ][0-9].*/", $val))
{
// Date line
$shout[$i]['date'] = str_replace("[Date]", "", $val);
}
if (preg_match("/\[Msg\] .*/", $val))
{
// Message line
$shout[$i]['message'] = str_replace("[Msg]", "", $val);
$i++;
}
}
// Have shouts going from the top-down
$i = 0;
$shout = @array_reverse($shout);
if (isset($shout))
{
// If shouts exist
foreach ($shout as $val => $key)
{
// Limit shouts according to $amount
if ($i == $amount && $i != 0)
{
}
else
{
// Create shout HTML
$timeadd = (3600 * $timemod);
$moddate = date($date, $key['date'] + $timeadd); // Add time modifier to date
$temphtml = str_replace("{date}", $moddate, $shouthtml);
$temphtml = str_replace("{message}", $key['message'], $temphtml);
$allshouts .= $temphtml;
$i++;
}
}
}
// Below: Handle all formatting
// Format HTML to be outputted
$html = str_replace("{shouts}", $allshouts, $html);
$html = str_replace("{errors}", "", $html);
echo $html;
?>
I need to change it so that the message entering form always stays at the top. Any idea how to do this? Thanks.