|
TmP's news engine
|
Principle
Once again, it is a matter of querying a MySQL database.
The SQL table that contains the news is pretty simple. It only has 6 columns: Date, Text, Source, Link, Title and ID.
Page navigation is handled the same way it is in the guest book, but there are only 5
messages per page.
News are added in the database thanks to a simple form and a PHP script.
Implementing the system
The first thing to do is a simple SQL query to the MySQL database and a calculation to guess the number of
the page to display:
$query = "SELECT * FROM news ORDER BY Date DESC";
$result = mysql_query($query);
// Number of messages
$nbr = mysql_num_rows($result);
// If we ask for a page beyond what the database contains,
// we provide the last page
if ($page*5+5 > $nbr) {
$page = ($nbr-$nbr%5)/5;
}
|
At the beginning, we have retrieved the page number passed as an argument:
parse_str(getenv("QUERY_STRING"));
if (($page == "") || ($page < 0)) {
// Page numbering starts from 0
$page = 0;
}
|
Then, it is only a matter of page layout:
// We retrieve the news to be displaid
// according to the page we want to access
for($i = $page*5; ($i < $nbr) && ($i < $page*5+5); $i++) {
if (! mysql_data_seek($result, $i)) {
continue;
}
if (! $row = mysql_fetch_row($result)) {
continue;
}
// We display news
list($year, $month, $day) = split("-", substr($row[0], 0, 10));
$news = $row[1];
$source = $row[2];
$link = $row[3];
$title = $row[4];
echo "<table...
...
}
|
At the end, we just have to create links to the previous and next pages.
Adding a news
We need 2 other PHP pages: one in which we fill in a form, and another one that performs the SQL query to
add the news in the database.
The first page is very simple. We need PHP only to automatically calculate the date of the day:
<FORM name="news" action="addnews.php3" method="post">
Date :<br>
<?
$date = date("d/m/Y", time());
echo " <INPUT name=\"date\" maxlength=\"10\" type=\"text\"";
echo " size=\"15\" value=\"$date\"><BR>\n";
?>
Title:<BR>
<INPUT name="title" maxlength="255" type="text" size="60"><BR>
Text:<BR>
<TEXTAREA name="text" cols="60" rows="10"></TEXTAREA><BR>
Link:<BR>
<INPUT name="link" maxlength="255" type="text" size="60"><BR>
Source :<BR>
<TEXTAREA name="source" cols="60" rows="10"></TEXTAREA><BR>
<INPUT type="submit" value="Add">
<INPUT type="reset" value="Reset">
</FORM>
|
The second PHP script (addnews.php3) does a simple INSERT query:
<?
// We must convert the date into a real time stamp
$date = ereg_replace("(.+)/(.+)/(.+)", "\\3\\2\\1"."000000", $date);
// We prepare the connection to the MySQL database
$host = "mysql_server";
$user = "database_user";
$password = "password";
// Connection
mysql_connect($host, $user, $password)
or die ("cannot connect to the MySQL server");
mysql_select_db($bdd)
or die ("cannot connect to the database");
// Query to add the news
$query = "INSERT INTO news (Date, Text, Source, Link, Title) ";
$query .= "VALUES ($date, \"$text\", \"$source\", \"$link\", \"$title\")";
mysql_query($query) or die ("cannot insert the news");
// We close the connection
mysql_close();
?>
|
download the complete code.
printable format
|