Today in my blockbuster tutorial, I am going to teach you how to create your own dynamic (Content management system) blog website.
Before beginning, let me tell you the basic skills you’ll need:
- CSS
- JavaScript (optional)
- PHP
- MySQL
- HTML
Let’s categorize the jobs to be done to make perfect, or at least, good CMS.
- Basic layout of your site
- Appropriate modules to divide the layout
- Using PHP to access MySQL.
After following this tutorial you’ll be able to:
- Create a main blog page;
- A page with list of blog contents;
- create dynamically generated contents.
Create a basic layout for your blog using HTML programming language. You may use Dreamweaver or similar program to layout your HTML. Later you can transfer it to PHP.
Now create and include the appropriate modules to your HTML layout. Refer to my previous article about adding modules to your blog.
Now after you’ve your layout and blog format ready we are heading towards PHP and MySQL.
Get clear with the basic PHP and MySQL methods before beginning with the programming.
If you have confusion comment here in my blog or refer to w3schools.com
First install an apache server to your desktop so that you won’t need to upload it to your webhost. This makes your work a lot easier and faster.
First we’ll be creating a database where the contents will be added and can be retrieved dynamically later whenever you need it. We’ll be creating it in notepad and naming it as, “create_db.php”.
// Connect to database
$con = mysql_connect(“localhost”, “user”, “pwd”)
// create a new database in your local host
$dbase=”CREATE DATABASE dbname”;
Mysql_query($dbase, $con);
// Select the database that we’ve just created
Mysql_select_db(“dbname”, $con)
//create a new table with columns in it
$table=”CREATE TABLE table_name
(
postID INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (postID),
Title MEDIUMTEXT,
Post LONGTEXT
)”;
//Close the connection after creating necessary database, table and column
Mysql_close($con);
Put the above code inside <?php ….the above code goes here…. ?>
I’ve already added comment the functions above it. But even though let me clarify you.
To connect to mysql mysql_connect() is used. Inside the mysql_connect function, host, username and password will be assigned in the following syntax.
Mysql_connect(“host_name”, “username”, “password”)
Note that the username and password is of your database not your control panel. The host name varies according to your web host. If you are using apache, then it’s usually localhost.
Mysql_query() function sends query to the database. This command can input the data, read the data, delete the data and also can edit the data in your MySQL table.
The syntax for it is mysql_query(function, connection)
Function refers to the MySQL command to create, destroy, read or edit the data. Connection refers to communicate with the database or say accessing the database. You can put variables like $function and $connection in place of function and connection in the above syntax.
Later you can create new variable $function and $connection for it.
$function carries the variable to command the MySQL.
CREATE DATABASE Database_name
The above is the syntax to create a new database named Database_name.
Similarly, to create new table with columns, use the following syntax.
CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
column_name3 data_type,
)Mysql_select_db() syntax selects the database with the name specified and the communication link given.
Mysql_select_db(“db_name”, $connection)
Mysql_close($connection) closes the connection.
Finally, we’ve our new database creating application ready.
Now we are creating article posting platform by creating a simple form with Title, Content and a submit button.
<form action=”post.php” method=”post”>
<table>
<tr>
<td><input type=”submit” /></td>
</tr>
<tr>
<td>Title</td>
<td><input type=”text”></td>
</tr>
<tr>
<td>Post</td>
<td><textarea rows=”10″ cols=”50″></textarea></td>
</tr>
</table>
</form>
In the above HTML code, I assigned an action to post.php with method POST applied.
In PHP you can apply either GET or POST method. GET method enable you to view the input items on your address bar while POST method doesn’t show it in address bar and it is invisible to users. POST is mostly used in password and other confidential inputs, GET is mostly used in hyperlinking. For example, the http:///www.website.com/index.php?page=1 that you see in most of the website uses GET where the name for the input is given page and the value is numerical.
We’ve the form ready you can save it as post.php.
Now lets code the PHP to read the inputs and post it to the MySQL database.
<?php
// if either Title or Content field is left empty and submit button is pressed, it will load the message, Please write the article with proper topic and press submit button.
if (empty($_POST[“title”]) || empty($_POST[“post”]) && isset($_POST[“save”]))
{
echo “Please write the article with proper topic and press submit button.”;
}
// if article is posted with proper credentials it will remove unnecessary and confusion values like quotes before the topic. This will enable to reduce mysql errors of double quotes.
if (isset($_POST[“save”]) && $_POST[“title”] && $_POST[“post”])
{
$title = $_POST[“title”];
$post = $_POST[“post”];
}
if (!get_magic_quotes_gpc)
{
$title= addslashes($title);
$post=addslashes($post);
}
// after cleaning the quotes, connects to the database and posts the title and content to the database. Using INSERT INTO table_name(Topic_column, Content_content column) VALUES ($title, $post)
if (isset($_POST[“save”]) && $_POST[“title”] && $_POST[“post”]){
$con= mysql_connect(“localhost”, “root”);
mysql_select_db(“News”, $con);
$write = “INSERT INTO table_name (Topic, post)
VALUES (‘$title’, ‘$post’)”;
mysql_query($write, $con);
mysql_close($con);
//if posted, it will show you, your article with the topic, (title you write) has been added to your database
echo “your article with the topic,”;
echo $title;
echo ” has been added to your database”;
}
//else it will show you message, you haven’t filled the Title and Post properly.
else
{ echo “You haven’t filled the Title and Post properly”;
}
?>
Copy and paste the above code just below the end tag of form, </FORM>, in post.php page.
Now we’ll need the page to show you contents.
Create a form to input the unique id of your article. Using input and applying GET method.
<form action=”view.php” method=”GET”>
<input>
<input>
</form>
<?php
//connects to the database
$con = mysql_connect(“localhost”, “username”, “password”);
//checks if you’ve already got the article id. If false it will load the lists of content with topic and hyperlink otherwise it will load the topic with specific id.
if(!isset($_GET[‘id’]))
{
mysql_select_db(“News”, $con);
$search = “SELECT postID, Topic FROM post ORDER BY postID”;
$result = mysql_query($search) or die(‘Errorsss : ‘ . mysql_error());
$id = $_GET[‘id’];
// create the article list
echo ‘<ul>’;
while($row = mysql_fetch_assoc($result))
{
//reads the postID and Topic from the respective column and set it in the variable $ideal and $topical respectively.
$ideal=$row[‘postID’];
$topical=$row[‘Topic’];
//creates hyperlink to respective article.
echo ‘<li><a href=”view.php?id=’.$ideal.'”>’.$topical.'<a></li>’;
}
echo ‘</ul>’;
mysql_free_result($result);
// else if you’ve put the value for the postID, it will load whole content with topic.
} elseif(isset($_GET[‘id’])) {
mysql_select_db(“News”, $con);
// get the article info from database
$query = “SELECT Topic, post FROM post WHERE postID=”.$_GET[‘id’];
$result = mysql_query($query) or die(‘Error : ‘ . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$title = $row[‘Topic’];
$content = $row[‘post’];
echo $title;
echo (“<br />”);
echo $content;
}
?>
And this is done, you have your simple blog ready. For better user interface, first layout all your pages, post.php and View.php properly and assign the respective code in order.
Hope this basic tutorial was a big relief for you. Please comment here if you have any confusion or else I will continue this tutorial with advanced and secured member logging system to secure the administrator’s page.
If you liked this tutorial share this tutorial using the link below: