Home | Scripts | Tutorials | Books | Hosting | Forums | Links|

Navigation:

Home
Source code
Tutorials
RSS feeds
Articles
Wordpress plugins
Books
Sample Chapters
Software
Downloads
Hosting
manuals
Script directory
Training
Our Links






 
 

SPONSORS



 
 

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.