Have you ever wanted to have one of those sites which displays the news in the middle of its index page , these usually have a title , a brief description , date , author and a URL . In this example we will create a database to store our news items which we will be able to add via an admin form which the webmaster will enter information . we will then display this info on our page , displayed by newest items . The reason I am building this is that latest news can be time consuming adding news items , removing news items etc so this should save some time. Anyway lets get on with it and create our database .
CREATE DATABASE news;
USE DATABASE news;
Now create a table like the following on the MySQL command line

Ok we will now create a form for you to enter your information , the info we want to submit is author , description , url and title . Here is the form we created .
<form action ="testingnews1.php" method = "post">
<table>
<tr><td>title</td><td><input type ="text" name = "title"></td></tr>
<tr><td>author</td><td><input type ="text" name = "author"></td></tr>
<tr><td>description</td>
<td><textarea name ="desc" rows = "5" cols = "50" wrap="VIRTUAL"></textarea></td></tr>
<tr><td>url</td><td><input type ="text" name ="url"></td></tr>
<tr><td><input type="submit"></td><td><input type = "reset"></td></tr>
</table>
</form>
This will create our form , note in this example we have called the script testingnews1.php.
In the next part we will log the information and display the links.
News Builder
Welcome to part 2 , in this part we will log our information from our form into our news database and we will also display our news items .
<?php
//connect using our host , username and password info
$connection = mysql_connect("localhost" , "username" , "password");
//select the news database
$db = mysql_select_db("news" , $connection);
//create or query inserting the appropriate values into the mysql
//table
$query = "INSERT INTO news(title , description ,author , submitted , url)
VALUES('$title','$desc','$author', now(),'$url')";
//execute query and store result as variable
$result = mysql_query($query);
//displays a nessage , you could redirect back to the form
//instead , if you wished
echo ("news item entered");
?>
In the code above we simply log the details from the form and store the info in the appropriate field . For the submitted field we use now() to get the date . Now that we have stored the information in the database we want to view this .
<?php
//connect using your hostname , username and password.
$connection = mysql_connect("localhost" , "username" , "password");
//select the news database
$db = mysql_select_db("news" , $connection);
//this is our SQL query , note we are ordering by the submitted date
//and setting a limit of ten items
$query = "SELECT * FROM news ORDER BY submitted DESC LIMIT 10";
//execute the query
$result = mysql_query($query);
//start creating our table
echo ("<table border = '0' width = '90%'>");
//loop through rows in the database
while ($rows = mysql_fetch_row($result))
{
//in the first row we display the title ($rows[1]) and the author $rows[3]
echo ("<tr bgcolor='violet'><td>$rows[1]</td><td>author : $rows[3]</td></tr>");
//in the next row we display the description which is $rows[2]
echo ("<tr><td colspan ='2' bgcolor='lavender'>$rows[2]</td></tr>");
//finally we display the url which is rows[5] and the submitted date which is rows[4]
echo ("<tr bgcolor='violet'><td><a href='$rows[5]'>$rows[5]</a></td><td>Submitted on : $rows[4]</td></tr>");
}
//finish or table
echo "</table>";
?>
This creates our table with news items , note that the color is just an example and can easily be changed . To change the amount of items to view change the LIMIT 10 to another value . Here is what the links looked like running on our machine , note how the items are ordered by date you could order by author , url , title anything you wished.

discuss this topic to forum
