Forgot your password?

How to design make your own News System with PHP.

By Edited Sep 25, 2015 0 2

How to Build Your Own News System with PHP

In this article, we are going to connect to the database and create news table and dump news records. Then we are going to display the list of news items with a link to the news details page. In news details page, we'll show the full news. I assume that you know how to setup localhost, connect with the database and use queries to get records from the table. If you don't know how, then don't worry. We are going to go in the details too.

1. Create Database and Table

First of all, create a database table by login into phpmyadmin.

After you are set up, open up a browser and type, http://localhost/phpmyadmin/. This should open phpmyadmin for you.

Now, create a database, lets say 'my_news'. Then create four fields for your news system. The field would be news_id, news_title, news_desc, news_postdate, and news_postedby. If you would like a detail tutorial, please check out How to Create a Database and Table using phpmyadmin. Here's the code to create the table.

CREATE TABLE `cal_blogs` ( `news_id` int(11) NOT NULL auto_increment, `news_title` varchar(255) NOT NULL default '', `news_desc` text NOT NULL, `news_postdate` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, `news_postedby` varchar(255) NOT NULL default '', PRIMARY KEY (`news_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1

2. Now, let's Connect to the Database

Here's a little bit of code to connect to the database. I assume that you know the basics of MySQL. If not, check out this articles, How to Connect With Mysql Database using PHP.

Here are the variables, which are easy to use so that you can reuse them later on. Just change the values according to your settings.

$dbHost = 'localhost'; /* either or localhost or the name of the website. */ $dbUser = 'root'; /* User of the database */ $dbPass = ''; /* Your password goes here */ $dbName = 'my_news'; /* Your database name goes here */

Now, let's connect with the database

<? $dbConn = mysql_connect ($dbHost, $dbUser, $dbPass) or die ('MySQL connect failed. ' . mysql_error());
mysql_select_db($dbName) or die('Cannot select database. ' . mysql_error());

Now, save the file as 'db_conn.php' inside db folder, which should be under the main folder. Let's the the site name is mysite. Then the structure of folder should be 'mysite/db/db_conn.php'. Got it. If you have confusion, then check this othe site here. How to Make a Web Site Structure for Ease.

3. Dump data into 'my_news' table

INSERT INTO `my_news` (`news_id`, `news_title`, `news_desc`, `news_postdate`, `news_postedby`) VALUES (1, 'Holi is coming', 'Holi is coming and there is going to waters, colors and sweets. I love Holi', '2008-09-07 06:22:15', 'Tom Hanks');

This will post one data into the table. If you want more data in the table, just click 'Insert' link at the top menu bar in phpmyadmin, and add values for the respective fields. That's it. If you are fine with that then

4. Let's design the news main page and get the list of news with short desc

First of all, include the db_conn.php file in your new file named 'index.php' under 'mysite/news' folder. Now, notice that you have saved your db_conn.php file inside 'db' folder.

<?php /* include connection file */ include_one ("db/db_conn.php"); ?>
Then we query the database using mysql select command. Notice that in our table, there are more than one records. So, we use 'while' loop until we get all the news.

<?php $sql_select_my_news = " Select * from my_site ". " ORDER by news_postdate DESC "; $result_select_my_news = mysql_query($sql_select_my_news); while($row_news = mysql_fetch_array($result_select_my_news )){

/* Now, we use easy to use variables to store up the data. */ $news_id = $row_news['news_id']; $news_title = $row_news['news_title']; $news_desc = $row_news['news_desc'];

/* We are trying to use php substr function to strip the news description with 300 letters. */ if($news_desc){ $news_desc = substr($news_desc, 0, 300); } $news_postedby = $row_news['news_postedby']; $news_postdate = $row_news['news_postdate'];

Now, we display the title, desc, postdate and author of the news. Here's how we do it.

<h2><a href="newsDetails.php?news_id=<?php echo $news_id;?>"><?php echo $news_title;?></a></h2> <p>Posted by <?php echo $news_postedby; ?> on <?php echo $news_postdate;?> <p><?php echo $news_desc;?></p> <p><a href="newsDetails.php?news_id=<?php echo $news_id;?>">&raquo; read more</a></p>

Let's end the while loop.
<?php } ?>

5. Finally, let's Create News Detail page

The news details page will be the same except we won't be using 'while' loop and php substr function because we want to show the whole news and not strip the news. In the preview main page, notice that we have passed news id to the newsDetails.php page. So, we must store this news id and query the table to fetch the news record which has this news id and display them on the browser.

Here's how we do it. Please follow the comments inside the php code.

/* Include the database connection file */

/* Get the news id and store it to a variable */
$news_id = $_GET['news_id'];

/* Now, let's query the table with this news id */
$sql_get_news_det = " Select * from my_news ".
" WHERE news_id = ".$news_id."";
$result_ get_news_det = db_query($sql_ get_news_det);

/* There is only one record with this new id, so we don't have to use while loop as in the previous page. */
$row_news_det = &dbFetchArray($result_ get_news_det);

/* Let us store the row of news data into easy accessible variables */
$news_id = $row_news_det['news_id'];
$news_title = $row_news_det['news_title'];
$news_desc = $row_news_det['news_desc'];
$news_postedby = $row_news_det['news_postedby'];
$news_postdate = $row_news_det['news_postdate'];

Now, let us display the records.
<h2<?php echo $news_title;?> </h2>
<p>Posted by <?php echo $news_postedby; ?> on <?php echo $news_postdate;?> <p><?php echo $news_desc;?></p>

That's it. You have created a news system for you. Please leave a comment so that i can help you in the future. I will also upload the script so that you could use it for free. Thanks for reading this article. If you like it, please share it with your friends or leave me a comment. Thank you so much.
You can also check out other similar articles. Happy browsing.



Mar 18, 2011 8:42am
Interest but complicated to me! I work with a lot of pageant girls who need current events constantly and this is something that would help them.
Mar 20, 2011 3:10am
Thanks for your response westernmom. I am glad that this article would help girls out there. This is just the first article i wrote in hurry. So forgive me for not being clear. I'll edit and write even consice clear article next time. Thanks. By the way, i am also writing on topics such as SEO, PHP, CSS, Photoshop. Please keep on following here. Thank you so much .
Add a new comment - No HTML
You must be logged in and verified to post a comment. Please log in or sign up to comment.

Explore InfoBarrel

Auto Business & Money Entertainment Environment Health History Home & Garden InfoBarrel University Lifestyle Sports Technology Travel & Places
© Copyright 2008 - 2016 by Hinzie Media Inc. Terms of Service Privacy Policy XML Sitemap

Follow IB Technology