Wednesday, February 29, 2012

Why dont you create your own CMS???--Part 7

So we have done the following in our previous lessons.
  • setup xampp!
  • created 'stupid' database with essential tables.
  • coded all the essential classes (database,theme).
  • completed our engine.php script.
Hey we have completed many files,now its time to check whether we are in the right path???
Remember the functions.php in the 'main' directory!!
Let us define some functions in 'functions.php'.
  • Open the file in an editor and write the following code.
/***************************
Shows the title of the page

****************************/
function title($title)
{
echo '<title>'.SITENAME.' : '.$title.'</title>';
}

/***************************
if your module has title
use this function to show title of app inside app block

****************************/
function app_title($app_title)
{
echo '<div id="app_title">'.$app_title.'</div>';
echo '<div id="spacer"></div>';
}
/***********************************
get site logo
***************************************/

function get_site_logo()
{
global $theme;
return $theme->get_sitelogo();
}


The functions are described below
  1. title ==> TO DEFINE THE TITLE OF A PAGE.
  2. app_title ==> TO DEFINE THE TITLE OF APP.
  3. get_site_logo ==> RETURNS THE SITELOGO.
More functions will be included later...

Now its time to create our first theme 'default'.
Please download the code and extract it into 'themes' folder.

Now open the 'index.php' in the stupid folder.
Include our 'engine.php' there.

<div id="container">
<div id="wrapper">
<?php 
require 'main/engine.php';
?>
</div>
</div>


After doing all these,run xampp.
Go to http://localhost/stupid/ from your browser.
If all came in our way,you will see a congratulation message.Otherwise please comment here so that I can understand the details of the error.

Friday, February 24, 2012

Why dont you create your own CMS???--Part 6

For any vehicle to move,it is essential to have an engine.In our 'stupid' cms the 'engine.php' will serve as the engine.
  • Open stupid >>> main folder.
  • create a file and name it as engine.php.
  • create another file and name is as functions.php
  • open 'engine.php' in an editor and write the following code.
The functions.php will contain the simple and essential functions to perform a specific task.
We will complete our functions.php later.For time being we are going to concentrate on our engine.php.
<?php
include_once('functions.php');
include_once('config/base.php');
include_once(CLASSPATH.'/database_class.php');
include_once(CLASSPATH.'/theme_class.php');

global $db,$theme;

$db=new Database();
$theme=new Theme();

/**********************************/

/*******************************/
$theme->load_css();
$theme->load_index();
$theme->load_footer();

/*******************************
*******************************/
?>
Let me briefly describe the code.
First of all we icluded the functions and configuration file.
Then the essential classes were included.
After including the core files,we instantiated the classes using 'new' operator and assigned it to global variable '$db' and '$theme'.Remember the global $db used in theme_class.php.
$db=new Database();
$theme= new Theme();
The database connection will already be made.Now its time to load our theme.The codes
$theme->load_css();
$theme->load_index();
$theme->load_footer();
will do the work for us.I told you "thats the magic of oop".

In the next post we will create our first theme called "default".
It will be easy and great ,if you are familiar with CSS.
Stay tuned...............!

Why dont you create your own CMS???--Part 5

Theming

Theming had always been my challenging topics while creating my cms.Thanks to the social networking sites through which I learned my first lessons on how to manage themes for a site.Before we get into theming,just think about "what kind of look does our site require?".For easy understanding,I am just concentrating on a blog based cms where admin can post,edit blogs.
The theme for our cms will be
  • simple
  • easily editable
  • high speed loading
Our theme will be having
  • header -----  with a sitelogo.
  • body ---------- containing views from modules.
  • footer --------- to show credits.
We will create our first theme 'default' later.For time being, just have alook at the structure of our theme,
///////////////////////////////////////////////////////////
default/
          images/
                    site_logo.gif
          include/
                    header.php
                    footer.php
          index.php
          css.php

////////////////////////////////////////////////////////////

Now we can take a look at theme_class.php
  • open stupid >>> include directory.
  • create a file and name it as theme_class.php
  • open the file in an editor and write the following code.

<?php
class Theme  {
  var $active_theme;
    
  function Theme()
   {
    $this->active_theme=$this->get_active_theme();

   }
  
  function get_active_theme()
   { 
   global $db;
   $sql="SELECT * FROM settings";
   $res=$db->query($sql);
   $settings=$db->fetch_array($res);
   $active_theme=$settings['theme'];
    return THEMESPATH."/".$active_theme;
   }
  
  function set_active_theme($name)
   {
   global $db;
   $sql="UPDATE `settings` SET `theme`='".$name."' WHERE 1";
   if($db->query($sql))
          {
           return;
          }
   }
  
  function get_themes_list()
   {
   $themes = array();
   if ($handle = opendir(THEMESPATH))
    {
    while ($t = readdir($handle))
    {
    // must be directory and not begin with a .
    if (substr($t, 0, 1) !== '.' && is_dir(THEMESPATH . "/" . $t)) 
     {
     $themes[] = $t;
     }
    }
    }
   sort($themes);
   return $themes;
   }
      
  
  function get_sitelogo()
   {
   return BASE.'/'.$this->active_theme.'/images/site_logo.gif';
   }
       
      
  function load_index()
  {
   include $this->active_theme."/index.php";
   }
   
 
  function load_css()
   {
    include $this->active_theme."/css.php";
   
    }
   
   function load_footer()
   {
    include $this->active_theme."/include/footer.php";
    
   }
   
}

?>   

Here are the description of the methods used.
  1. Theme  ==>  constructor assigns variable active_theme with the name of the current active theme.
  2. get_active _theme ==>  gets the name of the current active theme from the database.
  3. set_active_theme ==>  sets the value of active_theme field in the settings table of database with the value of the arguements.
  4. get_themes_list ==>  gets all the names of themes available.
  5. get_site_logo  ==>  returns the url of the site logo.
  6. load_css ==> includes the css.php from the active theme.
  7. load_index ==> includes the index.php from the active theme.
  8. load_footer ==> includes the footer from the active theme.
In the methods used above , we uses a global variable '$db' (object of database class) which will be created later.

Tuesday, February 21, 2012

Why dont you create your own CMS???--Part 4

Creating the database class file

Here we are going to create a simple class file for carrying database functions.
  • Open stupid>>include directory.
  • create file and name it as 'database_class.php'.
In our database class file,we will have property named connection.Here is what our class file looks like.


<?php
class Database  {
 var $connection;
 
 function __construct()
 {
 $conn=@mysql_connect(DB_HOST,DB_USER,DB_PASS);
 if(!$conn)
  {
  die("Cannot connect to database");
  }
  else
  {
  $this->connection=$conn;
  $this->select_db(DB_NAME);
  //echo "connected";
  }
 }

 /**************************
   select db
 ***************************/
 private function select_db($db)
 {
 if(!@mysql_select_db($db,$this->connection))
  {
  die("Cannot select database");
  }
 else
  {
  //echo "yes I GOT IT!!!";
  }
 }
 
 /****************************
   QUERY
 *******************************/
 public function query($sql)
 {
 $result=@mysql_query($sql,$this->connection);
 if(!$result)
  {
  die("SQL Error");
  }
 else
  {
  return $result;
  }
 }

 /*****************************
   fetch object
 *******************************/
 public function fetch_obj($result)
 {
 return mysql_fetch_object($result);
 }
 
 public function fetch_array($result)
 {
 return mysql_fetch_array($result);
 }
 
 
 /******************************
   fetch number
 ****************************/
 public function fetch_num($result)
 {
 return mysql_num_rows($result);
 }
   
}

?>
Lets have a quick look at the functions (methods) used.

1] __construct()
              
There is no need of talking much about the constructor.This function is automatically called during class instantiation.In the constructor we are making a connection to database.The arguments in @mysql_connect() function are constants we defined in our 'base.php' file.If connection is established ,database is selected by calling the select_db method.

2] select_db()
                 
 This method selects the database

3] query()
                 
 To do a mysql query and return a result.

4]fetch_object(),fetch_array()
                 
 Both these methods have an argument which is usually the result returned from the query method.The former returns the result as an object,the latter as an array.

5]fetch_num()
                   
This method is used to return the number of rows affected.

THIS IS A SIMPLE CLASS FILE AND  NOT AN ADVANCED DATABASE CLASS FILE .
But you can use this for simple database operations.
The class instantiation will look like
$db= new Database();
We will instantiate all our class files in a single file.I just mentioned it as an example.So if we need to call any method in database class, we will use '->'
for example, to do a  query ,
$db->query("YOUR-QUERY-HERE");
In the next post we will have a look at our next class file 'theme_class.php' to handle themes.

Sunday, February 19, 2012

Why dont you create your own CMS???--Part 3

Lets create our database tables.


Before writing class file let us create tables on our 'stupid' database.
Haven't created the database!!!!!!!
Please create one with name 'stupid'.
We will have 2 tables in our database.One for storing the settings of our site and the other for storing modules data.Are you confused??
Don't worry I will provide the sql for you.
The sql for settings table is as follows:
====================================================================

CREATE TABLE IF NOT EXISTS `settings` (
  `sitename` varchar(60) NOT NULL DEFAULT 'My new CMS',
  `admin_username` varchar(40) NOT NULL DEFAULT 'admin',
  `admin_pass` varchar(40) NOT NULL DEFAULT 'admin',
  `theme` varchar(50) NOT NULL DEFAULT 'default'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

======================================================================
The field sitename stores the name of the CMS,field theme stores the name of the current active theme (default by default).
The other two fileds stores the
Now let us create modules table.
=====================================================================

CREATE TABLE IF NOT EXISTS `modules` (
            `m_id` int(11) NOT NULL AUTO_INCREMENT,
            `name` varchar(256) NOT NULL,
            `active` tinyint(1) NOT NULL DEFAULT '0',
            PRIMARY KEY (`m_id`)
          ) ENGINE=InnoDB  DEFAULT CHARSET=latin1;

=====================================================================
The field name stores the module name,active field indicates whether the module is active.A module is active if field value od active is 1,inactive if the value is 0.
As I have already told you,we will be considering homepage and admin interface as modules.So their attributes have to be stored in modules table.
Note that they should be active.
===============================================================

INSERT INTO `modules` (`m_id`, `name`, `active`) VALUES
           (1, 'admin', 1),
           (2, 'home', 1);
=============================================================
For settings table

==============================================================
INSERT INTO `settings` (`sitename`, `admin_username`, `admin_pass`, `theme`) VALUES
('My new CMS', 'admin', 'admin', 'default');
==============================================================
In the upcoming posts we will create our essential classes required for our CMS.

Saturday, February 18, 2012

Why dont you create your own CMS???--Part 2

Working with configuratin files

So here we are going to work with 'main' folder where we will define our server settings and functions in a 'functions.php'.
  • Create a database named 'stupid'.
  • Open 'main' folder.
  • Create a new folder and name it as 'config'.
  • Create a file named 'base.php' inside config directory.
  • Open the file in an editor.
<?php
define('DB_HOST','localhost');
define('DB_USER','YOUR USER NAME');
define('DB_PASS','YOUR PASSWORD');
define('DB_NAME','stupid');
define('SITENAME','My first CMS');
define('BASE','http://localhost/stupid');
define('CLASSPATH','include');
define('THEMESPATH','themes');
define('MODULESPATH','modules');
?>

We defined constant DB_HOST and assigned a value 'localhost'.
BASE==>'YOUR WEB URL'
Other constant definition are self explanatory.
what are constants in php?
       
"A constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script . A constant is case-sensitive by default. By convention, constant identifiers are always uppercase."
eg:-    define('PI',3.14);
so you have finished the cofiguratin of your stupid cms.Next post we will be discussing about the classes required to create your own CMS.While I prepare the next post,you better refresh your knowledge about php classes.

Why dont you create your own CMS???--Part 1

INTRODUCTION

Well if you are ready with your base on php and mysql,we can start creating our own CMS.
Lets give our CMS a name - "stupid cms"
The features of our CMS will be

  • It will be based on object oriented programming.
  • it can be extended with module system.
  • Theming will be easy.
  • controlled from a single index page.
  • simple admin interface..etc
Let me remind you that stupid cms is just a cms to learn and will not be highly secure.
This cms might be very very bad for advanced developers,but will be good for beginners.
I am assuming that you have already set up xampp on your computer.
So lets staaaaaaaaaaaaaaarrrrrrrrrrrtttttttt..........

Stupid cms will be completely based on module system.Even home and admin interface are considered as modules.The default module will be "home"
  • Open htdocs folder in xampp directory.
  • Create a new folder and name it as 'stupid'.
  • Create following folders in stupid directory.
  1. Folders======>>  actions,modules,themes,include,main.
  2. Files========>>  index.php
  • Create a file 'actions.php' in actions folder.
  • The folder 'include' is for class files.
  • Folder 'main' is for configuration,functions files.
So now we can look to the 'main' folder.

Friday, February 17, 2012

Why dont you create your own CMS???

The title may seem strange to you.You might be thinking that I am a bit crazy to do so while I can get many.
Why should i bother you to create your own CMS??Am I crazy?????????

"Yeah,thats what I am!!"  
So we can get many content management systems freely from many developers like wordpress,drupal ....etc..The have many features and can be used in various ways to create your own site.These site can range from a simple blogging site to a more complex social networking site like facebook.
One of the main feature of a CMS is its admin interface through which the site admin can do whatever they wish their site to perform.Also they are extendable with modules.The look and feel can be varied with themes.
So simply a CMS are used to build dynamic sites in which content varies from time to time.
Its enough to talk about the merits and demerits (well they dont have many) of a CMS.Lets think about
why can't I create my own CMS???????
Can it be for a good or just a waste of time???
It might be a good way to furnish your base knowledge about web programming.And a better way of understanding the theory behind working of already established CMS.
               Most of the present CMS are based on php and Mysql.So I would prefer you to have a small knowledge about php and Mysql.
Are you SLEEPING??????????
Setup your computer as a server and
GET UP AND GO TO W3SCHOOLS
Now I think I should take a break.Enjoy learning php and mysql.

Thursday, February 16, 2012

Wordpress-king of blog CMS!

WordPress is a free and open source blogging tool and content management system (CMS) based on PHP and MySQL. It has many features including a plug-in architecture and a template system. WordPress is used by over 14.7% of Alexa Internet's "top 1 million" websites and as of August 2011 manages 22% of all new websites. WordPress is currently the most popular CMS in use on the Internet.
It was first released on May 27, 2003, by Matt Mullenweg as a fork of b2/cafelog. As of December 2011, version 3.0 had been downloaded over 65 million times.

Some of the features of wordpress are
  • Though wordpress is known for blogging,it can be extended to multiuser dynamic sites even a social networking site.
  • The look and feel of a wordpress site can be enhanced using various free and premium themes.
  •  Themes allow users to change the look and functionality of a WordPress website or installation without altering the informational content.
  • One very popular feature of WordPress is its rich plugin architecture which allows users and developers to extend its abilities beyond the features that are part of the base install; WordPress has a database of over 18,000 plugins with purposes ranging from SEO to adding widgets.
  • Widgets offer users drag-and-drop sidebar content placement and implementation of many plugins' extended abilities. Users can rearrange widgets without editing PHP or HTML code.
  • Wordpress is highly secure.

These are some of the common features of wordpress.For more click here..

To start working with wordpress,follow these simple instructions.
  1. Download latest wordpress from here.
  2. Unzip the zip file.
  3. Copy the contents of wordpress folder in htdocs folder of your  server.
  4. Follow simple instruction steps to complete your installation.
  5. To know more go to learn.wordpress.com

Wednesday, February 15, 2012

First steps towards web programming!

Last sunday,one of my friend asked me
    "Hey azy,which programming language would you prefer for me to enter into the world of web coding????????????"

This question might have been discussed over and over and over......in every discussion boards,forums etc.
Those discussions will never conclude .It goes on and on....
 But to me,there is only one answer and the answer is PHP .
Why are you all surprised,yes I said  P H P.
Let know more about php.

Introduction

Why should I bore you with stuffs copied from wikipedia and all.
Lets get straight into business.For working with php we need to setup a php environment on our computer.
To setup a php environment click here......

To create dynamic websites,we will also need mysql,apache...
So to setup up all these in your computer ,I prefer you to to install Xampp

You can download latest version of xampp from here...


For other click here....
Finish your installation and enjoy the magic.

Sunday, February 12, 2012

Codiverse....universe of codes

Well, its been a long journey of gathering knowledge.Moments of success,failure,we have gone through all the difficult paths.
 Now its time to share the knowledge with those who are eager to become a programmer,web developer,designer...

   Lets enjoy our universe of codes