# How To Make A News Script For MY Site With PHP?



## phpsharing (Dec 27, 2010)

Hi,programmer
I am a php beginner, I am looking for a news script.
What I'm looking for is to add an option form my main page that will 

display a news file. 
heres a sample
index.php--
Header
body
Todays News
footer
--end of file
I have found some php news script,but which one is suitable for me?
Any suggestion?
Thank you in advance!


----------



## pethichammer (Jan 15, 2011)

How to make a News script with PHP + MySQL



So..in this tutorial i will explain how to create PHP news script.

first we have to create the table in My SQL..

with the code below you will do this ( you have to enter it into SQL field ..in PHPmyADMIN)


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) ) 





>>>> script files

1 file will show the news ,1 file will contain variables and with the third one we can add news.

we'll call the first file "news.php"


<?php


$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.
{
echo "<br><table width='100%'><tr bgcolor='$title_cell_color'><td>
<img src='$bullet'><b>$title</b> posted on $date</td></tr>
<tr bgcolor='$news_cell_color'><td>$content</td></tr>
</table><br>";

}

?>



explaination:

1. inlcuding variables into the script

2. selecting the table in the db,formating date, limiting the number of news the script is going to show.

3. now the script is writing the data form db into tables.



now we have to make another file which can add new news into db.

we will call it "add.php"


<?php
include "var.php"; // 1.
if ($action==add) // 2.
{
$title=$_POST['title'];
$content=$_POST['content']; // 3.
mysql_query("insert into news (title,content) VALUES ('$title','$content')");
echo "<a href='index.php'>Home</a>"; // 4.
}
else // 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=\"textfield\">
</p>
<p>News content :
<textarea name=\"title\" cols=\"50\" rows=\"10\" id=\"title\"></textarea>
</p>
<p>
<input type=\"submit\" name=\"Submit\" value=\"Add\">
</p>
</div>
</form></td>
</tr>
</table>\n";
}


?>



explaination:

1. includeing variables into the script

2. if the $action id add..then the script is going to add data into the db,and if $action isn't selected,then the scipt is gonna show form for adding news.

3. the script is putting the data that we have entered into variables

4. if $action isn't set to "add" the script is showing a form for adding news



this is the third file....we are gonna define all important variables here..and we're gonna make a connection to db.


<?php
////////////////////////////////////////////////////////////////////////////////////////
$news_limit="xxxx"; // number of news that script shows
$user="xxxx"; // db username
$pass="xxxx"; // password
$db_name="xxxx"; // database name
$bullet="xxxx"; // path to the bullet image
$title_cell_color="xxxx"; // bg color for title cell in RGB...(black = #000000)
$news_cell_color="xxx"; // bg color for news cell in RGB...(black = #000000)
////////////////////////////////////////////////////////////////////////////////////////
$db = mysql_connect("localhost", "$user", "$pass");
mysql_select_db("$db_name", $db); //
?>



You have to change all the variables.


..if you have some questions are if you have found a mistake in this script...please let me know. 

Animation Institute in -Chandigarh


----------

