Skip to main content

Creating a simple Blog System – Part 1

Part 1 – Overview of Functions, Database & Files

Overview

In this tutorial we’ll be creating a very simple blog system. We won’t be using OOP yet in this tutorial. For creating a Blog using OOP in PHP, another more advanced tutorial will be written and posted as well. The same goes for creating a more advance CMS. However in this tutorial will just be creating a simple Blog system with php functions. Functions will be created for:

* Connecting to Host & DB
* Adding posts
* Deleting posts
* Adding replies
* Deleting replies
* Creating categories
* Retrieving & Displaying Posts
* Add user
* Edit user profile
* Display user profile
* Search

Also a simple 2 rows div layout will be created with a side-menu and main content div.

Database

Let’s start with creating the database for our simple blog. We’ll call it ‘simple_blog’. However you can call it anything you like as long as you set it correctly in the script later on. Now let’s create the tables inside this database.

Table: posts

The fields that need to be created:

[TABLE=6]

SQL:

CREATE TABLE IF NOT EXISTS `posts` (
`id` int(250) NOT NULL AUTO_INCREMENT,
`title` varchar(50) NOT NULL,
`author` int(250) NOT NULL,
`message` longtext NOT NULL,
`timestamp` int(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Table: replies

[TABLE=9]

CREATE TABLE IF NOT EXISTS `replies` (
`id` int(250) NOT NULL AUTO_INCREMENT,
`postid` int(250) NOT NULL,
`author` int(250) NOT NULL,
`message` mediumtext NOT NULL,
`timestamp` int(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Table: categories

[TABLE=10]

CREATE TABLE IF NOT EXISTS `categories` (
`id` int(250) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Table: members

[TABLE=11]

CREATE TABLE IF NOT EXISTS `members` (
`id` int(250) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
`email` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Files

Now we’ve created our database, so let’s start with an overview of the files we’ll be creating now.

* functions.php
* config.php
* profile.php
* index.php
* post.php
* includes/header.php
* includes/footer.php
* includes/sidebar.php
* admin/index.php
* admin/functions.php



Let’s start with creating our function to connect to the database we just made.

File: functions.php

<?php

function connect($connection) {

$host = $connection[‘host’];
$user = $connection[‘user’];
$pass = $connection[‘pass’];
$db = $connection[‘db’];
$conn = mysql_connect($host, $user, $pass);

If(!$conn)
die(“Couldn’t connect to host.”);

$db = mysql_select_db($db);

If(!$db)
die(“Couldn’t connect to database.”);

}

?>

Allright so first you see we set an argrument variable ‘$connection’ for the function. This variable should be given when calling the function and should contain all host & database info required to connect to the host & database. As you can see inside the function it seperates the sub-variables of the $connection variable into 4 new variables. These are for the host, user, password and database (db). As these are the data required to connect to the host & database and should be set in an array $connection and given to this function with sub-variable ‘host’, ‘user’, ‘pass’ and ‘db’.

We’ll be offering the $connection variable to the function as an array. It will use the sub-variable named ‘host’, ‘user’, ‘pass’ and ‘db’ to try to establish a connection to the host and database. So these we’ll need to set in our config.php file. We’ll shorten the name of the variable $connection to $conn. As the name of it doesn’t really matter as long as we give it to the function ‘connect’ when calling it.

File: config.php

<?php

####CONNECTION CONFIGURATION###
$conn[‘host’] = “localhost”; // database host (name/IP)
$conn[‘user’] = “root”; // database host username
$conn[‘pass’] = “password”; // database host password
$conn[‘db’] = “simple_blog”; //database name

?>

With this info our function ‘connect’ should be able to establish a connection to the host & database.

We set each sub-variable for the $conn array. So we’ve got one variable ( array ) that contains all sub-variables, all info required for establishing a connection to the database. Which our function connect will accomplish.

Let’s include these files to the index file already.

File: index.php

<?php

include(“functions.php”);

include(“config.php”);

?>

We can already use our function to connect to the host & database:

File: index.php

<?php

include(“functions.php”);

include(“config.php”);

connect($conn);

?>

We provide the array variable $conn to the function which contains all the sub-variables data of host & database ( as we set it in config.php ) required for establishing a connection.


Ref: http://www.webcodez.net/php-mysql/creating-a-simple-blog-system-part-1/

Comments

Popular posts from this blog

Learn phpfox

PHPFox  is a social network script, it is an internet application and when you install it, it is a website. The  phpfox  script comes in 3 packages, each with a different set of modules. it has two products: 1. Nebula (upto phpfox 3.8) 2. Neutron (Newer) You can check the demo on :  http://www.phpfox.com =================================================== To clear cache in phpfox follow the following steps, admincp >> Tools >> Maintenance >> Cache Manager >> Click on Clear All button =================================================== To work facebook app on local Following settings need to done in facebook app   1) go => setting => Advance 2) see "OAuth Settings" area and set "Valid OAuth redirect URIs" =  http:// projectdomain /index.php?do=/user/login/, http:// projectdomain .com/index.php?do=/user/register/, http:// projectdomain .com, http:// projectdomain .com/index.php 3) en...

Interview PHP

>> Why do you want to work at our company? Sir, It is a great privilege for anyone to work in a reputed company like yours. When I read about your company I found that my skills are matching your requirements.  Where I can showcase my technical skills to contribute to the company growth. >> What are your strengths? I am very much hard working and optimistic. Hard Working: Work with dedication and determination. Optimistic: work with positive attitude. I am a team player. I am also very hardworking, and will do what it takes to get the job done. >> What are your weaknesses? Gets nervous when talk to strangers I am a bit lazy about which I am not interested I tend to trust people too easily. I am working on it. >> Why should I hire you? With reference to my work experience, I satisfy all the requirement for this job. I am sincere with my work and would never let you down in anyway. I promise you will never regret for the decision to a...

How to Make Your Own PHP Captcha Generator

In this article we will create file based simple yet successful captcha generator. 3 Major Anti-spamming techniques used? Mathematical Operation like Random number + Random Number = -> The user must specify the answer Random word -> User must type the word Random question -> Obvious one which the user should answer correctly [ex: Are you human?] How Captcha works? The captcha generator generates an IMAGE with the question and then put up a session variable storing the value. User input though an input box. Using php POST, we compare the session variable data with the user input and tell whether its a bot or human. Its coding time The Code First let's write the php script which generates the captcha image. We use the simple header-content change technique, from which we can easily bring up an image from a given text. captcha.php PHP Code: array("Num" => "Num"), 1 => array("Are y...