Monday 17 October 2016

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.


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 –


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 –


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 –


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.

Source - Wizblogger

No comments:

Post a Comment