Our Affiliates
Have you ever wanted to create a dynamic news manager? Instead of updating those html files, then uploading everytime you want to add some news? In this tutorial we are going to create something which will hopefully make news publishing more effecient. Make sure you have access to php and mysql.
First we need to create our submit page. Start by creating a document and calling it addnews.php, and insert the following into it...
<form action="submit.php" method="post">
<b>Title</b>
<BR />
<input type="text" name="title" size="40" maxlength="80" value="" />
<br />
<br />
<b>News</b><BR><textarea name="news" rows="3" cols="40"></textarea>
<br />
<br />
<input type="submit" value="submit" /> <input type="reset" value="reset" />
</form>
This will be our form, where you can write out your news, to be displayed on your homepage. After you fill out the form, the data is sent to submit.php where it will be inserted into the database.
Create another document and name is submit.php, then insert the following into it...
<?
//grabs the variables
$news = $_POST["news"];
$title = $_POST["title"];
//gets mysql info
include("dbconnect.php");
//gets the current date...
$date = date("j F");
$addnews =MYSQL_QUERY("INSERT INTO news (id,title,date,news)". "VALUES ('NULL', '$title', '$date', '$news')");
//success...
echo("News Added!");
?>
Ok, the first part gets the mysql info from dbconnect.php, which we will be constructing in few seconds. It then inserts the news into the dbase, then prints a confirmation. We now have to create the mysql tables. Navigate to your phpMyAdmin, and execute the following SQL query...
CREATE TABLE `news` (
`id` TINYINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`title` TEXT NOT NULL,
`date` TEXT NOT NULL,
`news` TEXT NOT NULL
);
This will create the mysql table our news script needs. The next step is to create dbconect.php which will connect to your mysql database. Make a new document and call it dbconnect.php, and insert the following inside...
<?
mysql_connect("localhost","username","password");
mysql_select_db("databasename");
?>
You will need to replace 'username', 'password' and 'databasename' with your own mysql values, if you are unsure of these, you will need to contact your host. By now, everything should insert into the database, but we still need to print the news from the database onto your websites homepage (the last step). Create a new document called news.php, and insert the following into it...
<?
include("dbconnect.php");
$getnews = mysql_query("select * from news ORDER BY id DESC");
while($r=mysql_fetch_array($getnews)){
extract($r);
echo("<b>$title on $date<BR><BR>$news</b>");
}
?>
Now upload all the files to your server, and test it out! Obviously you will have format the way the news prints out, so that it fits in with your homepage.
First we need to create our submit page. Start by creating a document and calling it addnews.php, and insert the following into it...
<form action="submit.php" method="post">
<b>Title</b>
<BR />
<input type="text" name="title" size="40" maxlength="80" value="" />
<br />
<br />
<b>News</b><BR><textarea name="news" rows="3" cols="40"></textarea>
<br />
<br />
<input type="submit" value="submit" /> <input type="reset" value="reset" />
</form>
This will be our form, where you can write out your news, to be displayed on your homepage. After you fill out the form, the data is sent to submit.php where it will be inserted into the database.
Create another document and name is submit.php, then insert the following into it...
<?
//grabs the variables
$news = $_POST["news"];
$title = $_POST["title"];
//gets mysql info
include("dbconnect.php");
//gets the current date...
$date = date("j F");
$addnews =MYSQL_QUERY("INSERT INTO news (id,title,date,news)". "VALUES ('NULL', '$title', '$date', '$news')");
//success...
echo("News Added!");
?>
Ok, the first part gets the mysql info from dbconnect.php, which we will be constructing in few seconds. It then inserts the news into the dbase, then prints a confirmation. We now have to create the mysql tables. Navigate to your phpMyAdmin, and execute the following SQL query...
CREATE TABLE `news` (
`id` TINYINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`title` TEXT NOT NULL,
`date` TEXT NOT NULL,
`news` TEXT NOT NULL
);
This will create the mysql table our news script needs. The next step is to create dbconect.php which will connect to your mysql database. Make a new document and call it dbconnect.php, and insert the following inside...
<?
mysql_connect("localhost","username","password");
mysql_select_db("databasename");
?>
You will need to replace 'username', 'password' and 'databasename' with your own mysql values, if you are unsure of these, you will need to contact your host. By now, everything should insert into the database, but we still need to print the news from the database onto your websites homepage (the last step). Create a new document called news.php, and insert the following into it...
<?
include("dbconnect.php");
$getnews = mysql_query("select * from news ORDER BY id DESC");
while($r=mysql_fetch_array($getnews)){
extract($r);
echo("<b>$title on $date<BR><BR>$news</b>");
}
?>
Now upload all the files to your server, and test it out! Obviously you will have format the way the news prints out, so that it fits in with your homepage.



