NOTE TO ALL PEOPLE WHO TAKE THIS TUTORIAL! This is not a script premade to be compatible with your site! You cannot just copy paste this into your website and expect it to work. Throughout this tutorial, I explain how to set up a news system similar to the one I have coded at GreyCobra.com. You need to actually READ the tutorial in order to understand how it works. If you do not read it, I will not be as willing to help you with it if you run into problems!
1. The GreyCobra News system is a mySQL Database driven CMS. What's a CMS? A CMS is short for Content Management System, and allows users to add, modify, and remove content from a database without actually going to the database. A very simple form of a small CMS is the comment system at the bottom of this tutorial. It allows users to post directly to the database, and this site automatically displays the correct information. The big secret to all of this lies in Queries in PHP. What's a Query? A query is something that request information (or grabs data) out of a database. In order to query effectively, we need to set up something called a Table in our database (A table is a specific group of data in a database), and then split up our table into even more specific pieces of Data Called Columns. A column holds the most specific form of our data. In the GreyCobra News System, there is a column for Date, Title, Summary, User, Email, etc. In other words, columns divide different parts of your data. The last part of the table is the row. For every entry in our database, that equals one row. A row contains data for all of our columns, and our columns divide this data. This may be difficult to understand, so here is a diagram:
TABLE NEWS EXAMPLE
Column 1 |
Column 2 |
Column 3 |
Column 4 |
|
Title |
Date |
Summary |
User |
|
| Row #1 | News 1 |
Sep-12 |
Summary here |
z77 |
| Row #2 | News 2 |
Sep-13 |
Summary here |
z77 |
| Row #3 | News 3 |
Sep-14 |
Summary here |
Kunal |
As you can see, this is just an example Table titled TABLE NEWS EXAMPLE. Our example table is divided into 4 columns (Title, Date, Summary, and User). Whenever an entry is added, a row is added. See how Row #1 is titled News 1, Date is Sep-12, Summary is Summary here, and the user is z77. You can have virtually as many rows as you want, so adding news is never a problem (unless you happen to go over 4 quadrillion rows, which usually is no likely).
Ok, now that you have a basic understanding of how this all works, lets Create our table. Go into your phpMyAdmin, and select the database you want to add your table to. Run a SQL Query with the following:
|
CREATE TABLE `mynews` (
`id` int(11) NOT NULL auto_increment, `title` varchar(255) NOT NULL default '', `user` varchar(22) NOT NULL default '', `date` varchar(50) NOT NULL default '0000-00-00', `message` text NOT NULL, PRIMARY KEY (`id`) ) TYPE=MyISAM AUTO_INCREMENT=1; |
2. Ok, maybe you have done an SQL Query before, maybe not. What did you just do? You just told the database to create a new table called mynews, with columns titled id, title, user, date, and message. You also told the id to increase automatically with every new row added. Lastly, you told your table to start at the 1st row. Now that we have a table set up in our database, we need to learn how to pull or grab information from it, and we need to learn how to send information to it. To do this, we need to make a few documents. 1 Document retrieves information from the database, 1 document connects to the database, and one document sends information to the database. The first document that we will create is the one that connects to your database. When you created your database, you created a username and password, or one was supplied to you by your host. In order to get information, we need to create a file that will log into the database using this information first. We will call this file dbconnect.php. Go ahead and open a text editor of your choice and name it dbconnect.php. It should contain the following information:
|
<?php
$username = "username"; //your username $password = "password"; //your password $host = "your.host.com usually localhost"; //your mySQL server $database = "databasename"; //The name of the database your table is in mysql_connect($host,$username,$password) or die("Error connecting to Database!<br>" . mysql_error()); //connect, but if there is a problem, display an error message telling why there is a problem mysql_select_db($database) or die("Cannot select database!<br>" . mysql_error()); //Choose the database from your mySQL server, but if there is a problem, display an error telling why ?> |
3. Quick recap of that file: Define variables containing your username, password, server, and database, then connect to the server, then connect to the database. Whenever we are making queries to our database now, we need to type include("dbconnect.php"); right after the <?php. This will grab all of the information from this file, so that you do not have to type all of this information in each file we create. Now we need to make a file that grabs all of this information from the database. This file will connect to the database, query the mynews table, and then put it into a form we can see when browsing the actual site. Create a new document called viewnews.php. Copy and paste the following code making edits as needed:
|
<?
include("dbconnect.php"); //include the file to connect to the database $getnews = mysql_query("SELECT * FROM mynews ORDER BY id DESC"); //query the database for all of the news while($r=mysql_fetch_array($getnews)){ //while there are rows in the table extract($r); //remove the $r so its just $variable echo("<hr> <font size=3>$title added on $date</font><br> <font size=1>Posted by $user</font><br> <font size=2>$message</font><p>"); } ?> |
4. This queries the database and puts all of the information into a variable called $getnews (anything with a $ is a variable). Then, while there are rows in the database, the echo command will display a news article for each one. This is what the while command is for. It is saying that while there is a row in $getnews, do a function. A function is anything that lies within two brackets ( { function }). As you can see, the while function begins right after it says $getnews)). Anything inside of this function is repeated for every row in your table. Then we see the echo command. This command shows actual data on the website. Everything in php is done on the server, so what we are actually seeing is just the actual data in the echo command, not the php that powers it. In our echo, we have a few more variables. These are the columns in our table. Anywhere you write $title, the title will appear, $date and the date will appear and so on. It will fill these variables with different information (ie: if row #1 title is 1, and row #2 title is 2, it will display 1 in the first news article, and 2 in the second).
The last file we need to create is the file that submits the news to the table in your database. We will call this file submit.php:
|
<?php
include("./dbconnect.php"); //include the file that connects to the database if(!empty($title)) { //if the title is not empty, than do this function $title = addslashes($title); $user = addslashes($user); $message = addslashes($message); $date = date("F j, Y"); //set up the date format $date2 = mktime(); $sql = "INSERT INTO mynews (id, title, user, message, date) VALUES ('NULL', '$title','$user','$message','$date')"; //Insert all of your information into the mynews table in your database $query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error()); echo "Database Updated."; } else { //However, if the title variable is empty, than do this function ?> <form name="news" method="post" action="<?php echo $PHP_SELF; ?>"> <p>Please fill out all of the following fields:</p> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="117"><font size="1">News Topic/Title*: </font> </td> <td width="577"> <font size="1"> <input type="text" name="title" size="50"> </font> </td> </tr> <tr> <td width="117"><font size="1">Username*:</font></td> <td width="577"> <font size="1"> <input type="text" name="user" size="50"> </font> </td> </tr> <tr> <td width="117"><font size="1">Message*:</font></td> <td width="577"> <font size="1"> <textarea name="message" rows=10 cols=43></textarea> </font> </td> </tr> </table> <p> <font size="1"> <input type="submit" name="Submit" value="Submit"></font> </p> </form> <?php } //end this function ?> |
6. Ok, what the crap did you just do? Its really a pretty basic code. The first new thing you probably notice is the if(!empty($title)). Anything with a ! in front of it will mean "There is no", or "Undefined". empty($title) means exactly what it says, the title variable is empty or undefined. Ok, confused? It is a bit confusing seeing that this basically says if the title is undefined undefined, but we have to look at it another way. "If the variable $title, is NOT (!) undefined" aka, if $title does contain information, then do this function ({)! That hopefully clears things up a bit! Now onto the rest. We see another new code in here, INSERT. INSERT is very similar to a query except instead of retrieving data from the table in our database, we are sending stuff to it. In this case, we are inserting the information for all of our columns of data which will = 1 row. The next new thing we see is } else {. Earlier in the script, we said "if". Well what ELSE do we want our script to do if there no data in the $title variable? We use this else command after the first if function to tell the script that if the title variable has not been filled out, we want it to do another funtion!
In this function, it displays a few text boxes. When you type into these boxes, and click submit, it will turn all of these boxes into variables. This means that the $title tag will be defined. When we are creating the forum, we set the action to <?php echo $PHP_SELF;?>, a default php command that reloads the current page keeping all of the data in the variables. So what happens when we reload the page with our $title variable containing data? When it goes through the if command, it sees that $title is defined, and inserts all of the columns into the database!
Thats all there is to it! Just a few simple scripts can make your life a lot easier when updating web sites! Good luck, and be sure to use our comment system if you have any trouble!












