Quakenet/#php Tutorial

Note: If you opened this page from an external URL pay attention that all chapters are linked together. Be sure you also read all prior chapters of this tutorial, otherwise you will miss relevant content explained before.

News script

  1. Writing your own news script
  2. Layout of the mysql table
  3. The news script
  4. Downside of the news script

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.

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.

Questions about the chapter

No questions

Back to Next to
Copyright © to the OPs of #php/QuakeNet Valid XHTML 1.0 Strict Valid CSS!