1. Writing your own news script
Our news script should be easy and prints the html code just quick'n'dirty. It will be implemented and shouldn't be loaded with include in other areas.
2. Layout of the mysql table
We delete the old table with phpMyAdmin or with
DROP TABLE news;. Now we think about the columns we need.
-
id- A column for identify a news. This column got the typeINTand get the attributes UNSIGNED, NOT NULL, AUTO_INCREMENT and PRIMARY KEY. -
title- This column got the title of the news and its type isVARCHAR(100), 100 chars should be enought. As you must supply a title it got the attribute NOT NULL. -
added- The time when the news was added, so it must be got the typeDATETIMEwith the attribute NOT NULL. Note that we cannot use the namedateas it is a reserved keyword in mysql (for the column typeDATE). -
content- This column got the real news text. Astextmight be a better thenTEXTis already a mysql keyword and shouldn't be used as column name. We set the type of this column toTEXTas we might want to add a longer news text. Again we use the NOT NULL attribute.
Note these NOT NULL doesn't prevent you from entering an empty string, it only prevents you from enter a empty value at all (these NULL values). This table can be created with phpMyAdmin or with the following sql query.
CREATE TABLE news (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(100) NOT NULL,
added DATETIME NOT NULL,
content TEXT NOT NULL
);
It looks like the old table but we want to start from the beginning.
3. The news script
Now we begin our news script. As always we increase the error reporing level to get all error messages.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
Then we open the connection to mysql.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// change to your login
$db = @new MySQLi('localhost', 'username', 'pass', 'dbname');
if (mysqli_connect_errno()) {
die ('Could not create a connection to the database: '.mysqli_connect_error());
}
?>
And now lets go. First we echo some html header stuff. We write them in a new file and load them with include.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// change to your login
$db = @new MySQLi('localhost', 'username', 'pass', 'dbname');
if (mysqli_connect_errno()) {
die ('Could not create a connection to the database: '.mysqli_connect_error());
}
include 'header.html'; // DOCTYPE, <html>, <head>, and the starting <body> tag
?>
Now we send our sql query and output the result with a loop.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// change to your login
$db = @new MySQLi('localhost', 'username', 'pass', 'dbname');
if (mysqli_connect_errno()) {
die ('Could not create a connection to the database: '.mysqli_connect_error());
}
include 'header.html'; // DOCTYPE, <html>, <head>, and the starting <body> tag
$sql = 'SELECT
title,
added,
content
FROM
news
ORDER BY
added DESC';
// "ORDER BY" to sort the news by the date, descending
$result = $db->query($sql);
if (!$result) {
die ('Query failed, query was: '.$sql."<br />\nError message: ".$db->error);
}
if (!$result->num_rows) {
echo '<p class="info">There are no news.</p>';
} else {
while ($row = $result->fetch_assoc()) {
echo '<h1>'.$row['title']."</h1>\n";
echo '<h2>'.$row['added']."</h2>\n";
echo '<p>'.$row['content']."</p>\n";
}
?>
And at the end we output the html footer with include.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// change to your login
$db = @new MySQLi('localhost', 'username', 'pass', 'dbname');
if (mysqli_connect_errno()) {
die ('Could not create a connection to the database: '.mysqli_connect_error());
}
include 'header.html'; // DOCTYPE, <html>, <head>, and the starting <body> tag
$sql = 'SELECT
title,
added,
content
FROM
news
ORDER BY
added DESC';
// "ORDER BY" to sort the news by the date, descending
$result = $db->query($sql);
if (!$result) {
die ('Query failed, query was: '.$sql."<br />\nError message: ".$db->error);
}
if (!$result->num_rows) {
echo '<p class="info">There are no news.</p>';
} else {
while ($row = $result->fetch_assoc()) {
echo '<h1>'.$row['title']."</h1>\n";
echo '<h2>'.$row['added']."</h2>\n";
echo '<p>'.$row['content']."</p>\n";
}
include 'footer.html'; // </body>, </html> and maybe some copyright notes
?>
Now we have written our news script.
4. Downside of the news script
At the moment we can only add new databases if we work at the database directly.
This can be done as always with phpMyAdmin. We just click on Insert
and enters the new data. The id column should be empty as this value
is generated automatically by AUTO_INCREMENT. At the
added select the function NOW() from the drop-down list.
As you see this is a very rudimentary without any user interactions like
news comments, but as written at the top it is an easy and simple news script.