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.
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.
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.
Now cut selected code from top & paste them into the top of menu.php file
Now cut the selected code & add them into the top of content.php file.
Add the selected code into the top of footer.php
Add this selected code into bottom of the footer.php
Now we include all the separated file into our index.php. Our index.php file code look like this.
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.
Now reload our project, we can see menu which we add in our database.
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 } ?>
Refresh our cms project we can see the categories come from database.
So, Finally we make dynamic menu & categories for our CMS.
Nice Describe the CMS
Thanks
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
Nice tutorial here especially for some of us that are new in PHP Programming! Thanks!
I cant resist myself from thanking you……
Thank you very much…very simply put up…thanks….:)
Please ,show the admin panel,,create dyanmic menu,blog
please can i have the database query in mysql instead of mysqli
I am yet to learn how to use mysqli
thank you
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
The mysql and mysqli function The same way. The only different is that you just have to replace your mysql with mysqli for instance mysql_fetch_assoc() will be mysqli_fetch_assoc(), mysql_num_row() will be mysqli_num_row(). Note both of the uses the same pattern of connecting and querying the database. And also mysqli can be use in oop and procedural pattern of coding
unable to downlaod…..
i want this full demo sir plsss