Friday, October 22, 2010

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/

No comments:

Post a Comment