Create a Content Management System With PHP and MySQLI Part-1

Hello guys ! In this Series W’ll Create A Basic Content Management System with PHP and MySQLI. Now this article I will try to explain how you can make a simple template (html & css) useful for CMS. I choose a template & now try to make it useful for CMS. Let’s start.

Please Download first Red Blog HTML Template from Below Link:

Download Red Blog HTML Template

Download Red Blog Project Code

This is my html template.

mainhtml

Now I will make this template useable for CMS.

First convert the index.html to index.php. Now by separate the menu, header, content, left bar & footer. And also save them in .php extension.

Open the index.php in Dreamweaver .Select Menu “<div#navarea>” from Dreamweaver design section & select the html code from the code section.

Menu Select

convertMenuCode

menuPHP

Cut the selected code open a new php file, delete all default code then paste the code which we cut from index.php file & save as menu.php.

As the same way we need to separate header, content, sidebar & footer & save them header.php, content.php, sidebar.php & footer.php accordingly.

Now our index.php look like this.

index1

Now cut selected code from top & paste them into the top of menu.php file

AddTopCodeIntoMenu

Now cut the selected code & add them into the top of content.php file.

ContentTopCode

Add the selected code into the top of footer.php

topFooterCode

Add this selected code into bottom of the footer.php

BottomFooterCode

Now we include all the separated file into our index.php. Our index.php file code look like this.

UsableCMS

There is no difference in our browser view.

Our Template is ready now.

After that I will create a database name “cms” and make two table “menu” & “Categories”. Because we make Dynamic top menu & categories for our CMS.

CREATE DATABASE cms

Create table “menu” & add some data.

CREATE TABLE IF NOT EXISTS `menu` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,

`content` text COLLATE utf8_unicode_ci NOT NULL,

`status` enum('Publish','Unpublish') COLLATE utf8_unicode_ci NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

INSERT INTO `menu` (`id`, `name`, `content`, `status`) VALUES

(1, 'About', 'This is a simple CMS', 'Publish'),

(2, 'Contact Us', 'Search us on blog.', 'Publish');

Now we create our “categories” table

CREATE TABLE IF NOT EXISTS `categories` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,

`status` enum('Publish','Unpublish') COLLATE utf8_unicode_ci NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=7 ;

INSERT INTO `categories` (`id`, `name`, `status`) VALUES

(1, 'Basic PHP', 'Publish'),

(2, 'PHP & MYSQLI', 'Publish'),

(3, 'Javascript', 'Publish'),

(4, 'HTML', 'Publish'),

(5, 'CSS', 'Publish');

In this stage we need to create a php file for connect database to our cms. I save the file as cms.php.

<?php

class cms

{

public $conn;

public function __construct()

{

$this->conn= mysqli_connect("localhost","root","","cms");

/*host,user,password,database*/

}

public function showMenu($table)

{

/*this method return all record which status is Publish from the table name that we pass as parameter*/

$query = mysqli_query($this->conn,"SELECT * FROM $table WHERE status='publish'");

return mysqli_fetch_all($query,MYSQLI_ASSOC);

}

}

$mycms = new cms;

?>

Now we write some code in menu.php file for display menu from database.

<?php

foreach($mycms->showMenu("menu") as $m)

{

extract($m);

?>

<li><a href="#"><?php echo $name; ?></a></li>

<?php

}

?>

We use our method showMenu() with the parameter table name menu inside the foreach loop. Which retrieve all the record from menu table and hold them into $m variable as a array.

We also use extract() function for convert array to index variable, which hold the value of that column. Then we print the value of name column inside the <li></li> tag.

menu.php

Now reload our project, we can see menu which we add in our database.

FinalMenu

Ok now we will make our categories dynamic. Let’s do it.

We write some code in sidebar.php.

<?php

foreach($mycms->showMenu("categories") as $m)

{

extract($m);

?>

<li><a href="#"><?php echo $name; ?></a></li>

<?php

}

?>

Categories

Refresh our cms project we can see the categories come from database.FinalCategory

So, Finally we make dynamic menu & categories for our CMS.

7 comments on “Create a Content Management System With PHP and MySQLI Part-1
  1. I think this not work. I try but dont work,when I copy your code in menu.php I can’t get from database About and Categories also i dont understand why you use variable $name in example when that variable not exist before nowhere

  2. Please can i have the database query in mysql instead of mysqli

    I am yet to learn how to use mysqli

    And how can i enable mysqli in my wampserver

    thank you

Leave a Reply

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