Thursday, 24 October 2013

Tutorial 6 : Array List implementation in PHP (Fetch a Array List values)

Dear Techie's,
                       Here we are in final step. in this tutorial i will explain you how to displaying mysql data using Array List.  and you can also fetch the values from multiple tables and set to same object to Array List.  in this way you can get all database values in one place using One iterator.

From tutorial 1 to 6. i have shared my works. i hope this tutorial will help you in many ways. 

Try yourself and you could do more and you could get more.

Here is code for displaying Mysql data using Array List.






user_list();
?>

If you like this, please dont forget to put your valuable comments. Happy Coding .......!

Tutorial 5 : Array List implementation in PHP (Passing a Mysql data to Setter methods)

Dear Techies,
                      This class is used to setting a mysql data to setter methods. this class having 2 setter method. and here i wrote short and efficient  code for setting the values to Array List.
and any one can easily understand the process of each lines.

Here is my code.

ConnectDB();
        //sql query
        $query = "select * from users";
        //query execution
        $result_set = mysql_query($query);
        //record count
        $record_count = mysql_num_rows($result_set);
        if ($record_count > 0) {
            //array list object
            $user_bean_list = new ArrayList();
             //looping record set
            while ($record_set = mysql_fetch_object($result_set)) {
                //user_Details_DTO object
                $user_bean = new user_Details_DTO();
                //setting a mysql database values
                $user_bean->set_user_id($record_set->user_id);
                $user_bean->set_user_name($record_set->name);
                //passing DTO object to arraylist 'add' method
                $user_bean_list->add($user_bean);
            }
            //returing objects
            return $user_bean_list;
        } else {
            //return false, if no record found.
            return 0;
        }
    }

}?>

Tutorial 4 : Array List implementation in PHP (Creation of DTO Class)

Dear Techie's,
                     I have shared basic resources of application in Tutorial 1 To 3. Those files are used to db connection , processing Array List etc. In this post i will do actual Array List implementation.

In java, most of java programmers use setter and getter methods to setting an getting a values. the class contains many setter getters for their values. and these methods wrapped inside a class. in  java ,for these classes we call 'DTO class'(Data Transfer Object) . In PHP, setter getter functionality is similar to java DTO class. 

Let's do practice,



user_id = $id;
    }

    // getter method : this method only returns the private variable value.        
    function get_user_id() {
        //returns a value of private variable $user_id

        return $this->user_id;
    }

    //same type of getter setter methos for variable $user_name.

    function set_user_name($name) {

        $this->user_name = $name;
    }

    function get_user_name() {

        return $this->user_name;
    }

}?>

Wednesday, 23 October 2013

Tutorial 3 : Array List implementation in PHP (Creation of Model Class using MVC)

Dear Techie's,
                    The model view controller(MVC) pattern is the most used pattern for today’s world web applications. The MVC pattern separates an application in 3 modules: Model, View and Controller


  • Model : The model is responsible to manage the data. it stores and retrieves entities used by an application, usually from a database, and contains the logic implemented by the application.
  • View : The view (presentation) is responsible to display the data provided by the model in a specific format. It has a similar usage with the template modules present in some popular web applications, like wordpress, joomla
  • Controller : The controller handles the model and view layers to work together. The controller receives a request from the client, invokes the model to perform the requested operations and sends the data to the View. The view formats the data to be presented to the user, in a web application as an html output. 

More Details : http://www.htmlgoodies.com/beyond/php/article.php/3912211

This is overview of MVC design pattern.  based on MVC structure i have created Model class. that is,


Class Name : MainModel.class.php

//import a dbconnection class
require_once 'dbConnection.class.php';
//class MainModel
class MainModel {
    
    //variables
    var $dbcon;
    public $error = '';
    
         //You call a constructor when you create a new instance of the class
       //'MainModel' containing the constructor.
      //ex : obj = new MainModel(); (PHP 4)

    //The "__construct" was introduced in PHP5 and it is the right way to define your, well, constructors
   // (in PHP4 you used the name of the class for a constructor). You are not required to define a 
 //constructor  in your class, but if you wish to pass any parameters on object construction then you 
//need one.  more info : http://php.net/manual/en/language.oop5.decon.php
    
    public function __construct() {
        //create '$dbcon' Object
        $this->dbcon = new dbConnection();
    }
    //database connection  method 
    public function ConnectDB() {
       //$this refers to the instance of the class 
       //when we use '$this->dbcon' it refers object of same class. and using this object we should call the connect method.
        $con = $this->dbcon->connect();
        if ($con) {
            return true;
        }
    }
    //database disconnect method
    function DisconnectDB($dbcloseval) {

        if ($this->dbcon->disconnect($dbcloseval) == false) {
            return false;
        } else {
            return true;
        }
    }

}?>

Tuesday, 22 October 2013

Tutorial 2 : Array List Implementation in PHP (Database Connection)

Dear Techie's

                    This is my database connection class. this class connecting mysql database. this class is very easily built and everyone can understand easily. so am not going to explain about db connection class.




conn) {
            $conn = @mysql_connect($this->db_host, $this->db_user, $this->db_password) or die("Could Not Possible To Connect a Server" . mysql_error());
            if ($conn) {
                $seldb = @mysql_select_db($this->db_name, $conn) or die("Could not Possible To Connect A Database" . mysql_error());
                if ($seldb) {
                    return $this->conn;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
        return $this->conn;
    }
    /* Close/Disconnect DB connection*/
    public function disconnect($dbconnected) {
        $disc = mysql_close($dbconnected);
        if ($disc) {
            return true;
        } else {
            return false;
        }
    }

}

Tutorial 1 : Array List Implementation using PHP

Dear techie's,
                    We all know that there is no built  array list class in PHP . Its not a mean that we cant implement Array list functionality in PHP . As of now PHP don't have array list class.  But still we can have a array list functionality in PHP

The Mike langue given good solution for array list. It does not work as java array list. but we can get good result from this array list class.

More details : http://www.icurtain.co.uk/161_PHP_ArrayList_Class

I have worked on this array list class and i wrote a standard class to display a database values using array list class.  its works great for me. so here i contribute my works. please make it useful.


To Implement Array list in php. we need to download the array list class.
Download Link : http://www.icurtain.co.uk/161_PHP_ArrayList_Class

Or
Copy and paste below code in your application.


arrayList[sizeof($this->arrayList)] = $item; 
        array_push($this->arrayList, $item);
    }

    public function addAtPos($position, $item) {
        if ($position < count($this->arrayList) && $position >= 0) {
            $this->add($item);
            $this->shift(count($this->arrayList) - 1, $position);
        } else {
            throw new Exception('List out of bounds');
        }
    }

    public function getList() {
        return $this->arrayList;
    }

    public function hasValue() {
        if (isset($this->arrayList[$this->pointer])) {
            return true;
        } else {
            return false;
        }
    }

    public function hasNext() {
        if ($this->pointer <= count($this->arrayList) - 1) {
            return true;
        } else {
            return false;
        }
    }

    public function next() {
        if (isset($this->arrayList[$this->pointer])) {
            //return $this->arrayList[($this->pointer++)-1] = $value; 
            $this->pointer++;
            return($this->arrayList[$this->pointer - 1]);
        } else {
            return null;
        }
    }

    public function shift($origin, $dest) {
        //wont shift from last element 
        if ($origin > count($this->arrayList) || $origin < 0 || $dest > count($this->arrayList) || $dest < 0) {
            throw new Exception('List out of bounds');
        }
        if ($origin > $dest) {
            $temp = $this->arrayList[$origin];
            $this->shiftUp($origin, $dest);
            $this->arrayList[$dest] = $temp;
        } else {
            $temp = $this->arrayList[$origin];
            $this->shiftDown($origin, $dest);
            $this->arrayList[$dest] = $temp;
        }
    }

    private function shiftUp($origin, $dest) {
        for ($i = $origin; $i > $dest; $i--) {
            $this->arrayList[$i] = $this->arrayList[$i - 1];
        }
    }

    private function shiftDown($origin, $dest) {
        for ($i = $origin; $i < $dest; $i++) {
            $this->arrayList[$i] = $this->arrayList[$i + 1];
        }
    }

    public function remove($item) {
        if (array_key_exists($item, $this->arrayList)) {
            unset($this->arrayList[$item]);
        } else {
            throw new Exception('key not found');
        }
    }

    public function addArray($array) {
        foreach ($array as $item) {
            $this->add($item);
        }
    }

    public function reverse() {
        $this->arrayList = array_reverse($this->arrayList);
    }

    public function size() {
        return count($this->arrayList);
    }

    public function reset() {
        $this->pointer = 0;
    }

    public function end() {
        $this->pointer = count($this->arrayList) - 1;
    }
}

?>

Thursday, 17 October 2013

Represent a mysql data in Jqplot Pie Chart using PHP.


//JavaScript
var data = [
  ['Heavy Industry', 12],['Retail', 9], ['Light Industry', 14],
  ['Out of home', 16],['Commuting', 7], ['Orientation', 9]
];

to achieve above data format(example : 1)  programmers use a loops. and iterate a record set and generate jqplot data in the form of string type. (as i mentioned in example 2)

example  : 2
//PHP
$string.='["'.$row['name'].'",'.$row['id'].'],';
This is not good practice.

So i have written a class to represent a mysql data in jqplot. this class helps to programmers to achive (example : 1) format easily. 


Here is the code






//class jqplot 
class jqplot {
   
    function __construct() { 
        //constructor
    }

    function create_jqplot_data_array() {    
        //call db connection function
        //ex : $db->connect();
        
        //execute the query fetch table data
        //$sql = "select name,avg from 'table_name'"
        
        //create an array       
        $container = array();
        
    
        // loop a recordset and pass the field value to array ($container)
        while ($record = mysql_fetch_object($result)) {
            
            //passing value to '$container' array.           
            //intval() : Returns integer value.
            //for more details about intval() http://php.net/manual/en/function.intval.php
            $container[] = array($record->name, intval($record->avg));
        }
           //json_encode() : Returns the JSON representation of a value
           //for more details about json_encode() http://php.net/manual/en/function.json-encode.php
        $json_data = json_encode($container);
        // returns  json data.
        return $json_data;
    }
}
//create a object using jqplot class
$obj_jqplot = new jqplot();
//call a function : create_jqplot_data_array()
//it returns the encoded json data from the function
$json_data = $obj_jqplot->create_jqplot_data_array();
//display the json encoded data.
echo $json_data;

?>

Hope this helps.