Monday, November 8, 2010

Create a news feature webpage using PHP



Main function: add.php
1) Create a table news using MySQL

CREATE TABLE news 
( id int(10) unsigned NOT NULL auto_increment,
postdate timestamp(14) NOT NULL,
title varchar(50) NOT NULL default '',
content text NOT NULL, PRIMARY KEY (id),
KEY postdate (postdate), FULLTEXT KEY content (content) )


2)configDB.php

<?php////////////////////////////////////////////////////////////////////////////////////////
$news_limit="30"; // number of news that script shows
$dbHost = 'sql105.zoka.cc:3306';
$user="zokac_6523693; // db username
$pass="mypassword"; // password
$db_name=" zokac_6523693_signup"; // database name
$bullet="bullet.png"; // path to the bullet image
title_cell_color="#A9F5F2"; // bg color for title cell in RGB...(black = #000000)
$news_cell_color="#F2F2F2"; // bg color for news cell in RGB...(black = #000000)
$db = mysql_connect("$dbHost", "$user", "$pass")or die("Can not connect to MySQL server");
mysql_select_db("$db_name", $db) or die("Can not connect to database");; //
?>

3) add.php
<?php
include "configDB.php"; // 1.
if(isset($_REQUEST['submit'])) // 2.
{
$title=$_POST['title'];
$date=date( 'Y-m-d H:i:s');
$content=$_POST['content']; // 3.
//echo $title.$date.$content;
mysql_query("insert into news (postdate,title,content) VALUES ('$date','$title','$content')");

//echo "<a href='index.php'>Home</a>"; // 4.
}

print "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
<tr>
<td><form name=\"form1\" method=\"post\" action=\"add.php?action=add\">
<div align=\"center\">
<p>Title:
<input type=\"text\" name=\"title\">
</p>
<p>News content :
<textarea name=\"content\" cols=\"50\" rows=\"10\" id=\"title\"></textarea>
</p>
<p>
<input type=\"submit\" name=\"submit\" value=\"add\">
</p>
</div>
</form></td>
</tr>
</table>\n";

$query = "SELECT *," .
"DATE_FORMAT(postdate, '%Y-%m-%d') as date " .
"FROM news ORDER BY id DESC LIMIT $news_limit"; // 1.
$result = mysql_query($query);

while($r=mysql_fetch_array($result)) // 2.
{
$id=$r['id'];
$title=$r['title'];
$date=$r['postdate'];
$content=$r['content'];
echo "<br><img src='$bullet'/>Post number $id:<br><table width='100%' bgcolor='$title_cell_color'
><tr><td>
<b>$title</b> posted on $date</td></tr>
<tr  bgcolor='$news_cell_color'><td>$content</td></tr>
</table><br>";

}

?>
The result can be seen from HERE.

No comments:

Post a Comment