<pre class="prettyprint lang-php"><?php
// Set the name of the text file that will store the news items $filename = "news.txt";
// Check if the form was submitted if (isset($_POST['submit'])) { // Read the title and body from the form $title = $_POST['title']; $body = $_POST['body'];
// Append the title and body to the news file file_put_contents($filename, $title . "\n" . $body . "\n\n", FILE_APPEND); }
// Read the news items from the file and store them in an array $news = file($filename, FILE_IGNORE_NEW_LINES);
// Loop through the news array and display each news item for ($i = 0; $i < count($news); $i += 2) { echo "<h1>" . $news[$i] . "</h1>"; echo "<p>" . $news[$i + 1] . "</p>"; }
?> <form action="index.php" method="post"> <input type="text" name="title" placeholder="Title"> <textarea name="body" placeholder="Body"></textarea> <input type="submit" name="submit" value="Submit"> </form>