Home / TUTORIALS / How To Setup A Blog In PHP

How To Setup A Blog In PHP

PHP is basically one of the commonly used scripting language for web development. It is also known as a programming language.  One of the best things about this language is that it is very simple for all newcomers and with that it also offers various advanced features for some professional purpose. So with this superb programming language you can easily learn to write scripts in short time.

PHP is a very dynamic language which can be easily embedded with HTML coding. On the other hand, it can be used in combinations with various other web content management systems, web frameworks and web template systems.

The most important aspect of this language is that its code is processed by a PHP interpreter implemented in the form of modules in the particular web server. After that, a web server correlates and combines the result of the interpreted PHP code, which further can be in any type of data or images.

Below described are various steps to set up a dynamic blog in PHP:

1. Create a database

The first and the very basic step of starting a blog in PHP is to create a database to store the blog contents and before creating a database you should list out what all you need to start a blog. Some of the obvious things to start a blog is post id, title, author name and content.

create-a-database

Now we can move ahead and start creating one table with mysql. A table is usually created to hold the information and most likely to start a basic blog, but on the other hand, one table is not just enough to control more data. Like you can just store the name of the author in the same table in which you have created the blog post, but if you also want to store the author’s phone number or email then you have to add another field to your table.

After this make a separate table with any name suitable to you and in this you can store all the author’s information related to his name, emails, IDs and URLs. Once it is created, then in your blog post you will table you will be able to clearly point the person you want details about and this id will be known as a foreign key which will be a relationship between your blog table and the other table you have created above.

Below are the tables which needs when you create your own blog –

Posts
PostID (identity)
PostTitle (varchar)
PostDate (datetime)
Deleted (int)
OwnerID (int FK to Users)

PostDetails
PostDetailID (identity)
PostID (FK to Posts)
Sequence (int) -> for long posts you order by this
PostText (text)

Comments
CommentID (identity)
Comment (text)
CommenterID (int FK to Users)
CommentDate (datetime)
Deleted (int)

Users

UserID (identity)
UserNAme (varchar)
UserEmail (varchar)
CreatedDate (datetime)
Active (int)

 

Another very important thing you should have on your blog is a tag for each particular post. For this also you should have a strong database so that you can create different tables for all tags. Now to do this just create another table with any name suitable for you and it will hold two foreign keys, out of which one will be in the blog post and the other will be in a tag that the particular blog post is associated with. Therefore, by this way you can assign as many tags as you want to your blog.

2. Create objects and add blog posts in PHP

The second most important step of setting up a blog in PHP is to create objects and add different blog posts in it. Now for this you need to create objects in PHP that will handle all the data for you. Basically, objects play a very important role in the programming as they act as a medium  to pull together different variables and methods that relate to a same thing. On the other hand, objects will also help you to organize your programs in much easier way.

An object is defined as a class in PHP programming language and a class is the specific structure of each object or a programming language that is used as a blueprint to create various objects in the blog post. Till now, we have defined all the properties and now its time to define the term method. Methods are basically used to describe functions, but the main difference in this is that a particular method is the function inside the object. So you can say that all methods are also called as functions, but all functions are not methods.

So, the first method is a constructor which automatically calls up whenever you make a new entry of the blog post object. Therefore, you can create a new function called construct and can easily add various values in it like post, id, title and date. Further, for each of these variable you will put “in” before all the words so that you can differentiate functions with variables. After this you can process the data in PHP by using some simple loops in it.Till now your hardest part that is object class is complete and now all you have to do is set up your database connection to view your posts.

Below is example of creating objects and constructor –

classBlogPost
{
public$id;
public$title;
public$post;
public$author;
public$tags;
public$datePosted;

function__construct($inId=null, $inTitle=null, $inPost=null, $inPostFull=null, $inAuthorId=null, $inDatePosted=null)
{
$this->id = $inId;
$this->title = $inTitle;
$this->post = $inPost;
}
 }

3. Fetch the data and display it by using PHP

This is the last step of setting a blog in PHP which includes fetching up of data from Mysql database and display it by using PHP. But before you start doing anything, you need to set up a include.php file or folder to maintain references of your blog post and easily connect to your previous database. After this, just retrieve your blog posts from the database and for this you have to write a specific function. Once functions are written, the next step is to process the required data from each query and then add them to your blog. After this, you can display your data by just opening your index file and set up a basic HTML source code page. It will all start working now.

Here below is example of fetching data using sql query –

$query= mysql_query("SELECT first_name, last_name FROM People WHERE id = ". $inAuthorId);
$row= mysql_fetch_assoc($query);
$this->author = $row["first_name"] . " ". $row["last_name"];

Therefore, this is all about the set up of blog in PHP. With this post, you will definitely have a better understanding of working of various object oriented programming languages.

 

About the Author –

Kristina loves cooking and wants to start her own food blog. Recently, she has joined The Best Essay team as a part time food-n-recipe blog post content developer.

About Nawaf Gantare

Nawaf, An Eighteenth-something savvy Web Designer / Social Media Manager / SEO Strategist based In India. He is Passionate about Web Designing, Programming And Web Security.He is currently learning Computer Science and is in Second Year

Check Also

UTS QR codes for pune

QR code for Lonavla UTS QR code for Lonavla  station will help you book your …

UTS QR Codes for Mumbai Stations

Ever had an issue while searching for UTS QR code on the platform? Were you …

2 comments

  1. Thanks for sharing this cool article .

  2. Thanks for sharing, really like the post.

Leave a Reply

Your email address will not be published. Required fields are marked *