Friday, August 22, 2008

Get node count of a "Binary Tree"[Code For Use/db class/Common Function]

/////////////////////Config.php//////////////////////

$hostname_glb = "localhost";
$username_glb = "root";
$password_glb = "";
$database_glb = "sunmax";

$Glb_Node_Array = array();
$Glb_Stack = array();
$Glb_Top = 0;
$Glb_Ptr = NULL;

$Gbl_AllMemberCount=0;
?>
/////////////////////////////////////////////////////////////////
//////////////////////////dbcon_inc.php///////////////////////////////
/////////////////////////////////////////////////////////////////////

include("config.php");
include("classes/dbclass.php");
include("classes/common_functions.php");

$dbobj -> setup_conn($hostname_glb,$username_glb,$password_glb);
$dbcon = $dbobj -> db_connect();
$dbobj -> select_db($database_glb,$dbcon);
?>

/////////////////////////////////////////////////////////////////////
//////////////////////////End dbcon_inc.php///////////////////////
///////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////
///////////////// Code For Use////////////////////////
//////////////////////////////////////////////////////

include("dbcon_inc.php");
//echo $Gbl_AllMemberCount;
//$Gbl_AllMemberCount=0;
//$_SESSION['sess_mid']);//Root Node Id



global $Glb_Top,$Glb_Ptr,$Glb_Stack,$Glb_Node_Array,$Gbl_AllMemberCount;

$Glb_Top = 0;
$Glb_Stack[$Glb_Top]=NULL;
$Glb_Node_Array =array();
$Gbl_AllMemberCount=0;
$TempFlag = $_SESSION['sess_mid'];

if(strlen($TempFlag) > 0){

$Glb_Ptr=$TempFlag;
$cmobj -> get_all_child_node();
echo (int)($Gbl_AllMemberCount + 1);//this is node count
}else{

echo 0;
}


?>



///////////////////////////////////////////////////////////
///////////////// End Code For Use/////////////////////////
///////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////
////////// Common Class //////////////////////////////////
/////////////////////////////////////////////////////////

class common_function extends db{

var $sql = NULL;
var $res = NULL;
var $row = NULL;
var $num = NULL;


//New Function For Node Count
function get_child_root($MID,$LorR){

$this->sql = "SELECT MID FROM tbl_member WHERE SponserID = '$MID' AND Position = '$LorR'";
$this->res = mysql_query($this->sql)or die("1.Error in get_left_child function : ".mysql_error());
$this->num = mysql_num_rows($this->res);
if($this->num > 0){

return $this->get_child($this->res);
}else{

return;
}
}


function get_child($rs){

$this->row = mysql_fetch_object($rs);
return $this->row->MID;
}


function get_all_child_node(){

global $Glb_Top,$Glb_Ptr,$Glb_Stack,$Glb_Node_Array,$Gbl_AllMemberCount;

$this->sql = "SELECT MID FROM tbl_member WHERE SponserID = '$Glb_Ptr' AND Position = 'R'";
$this->res = mysql_query($this->sql)or die("1.Error in get_left_child function : ".mysql_error());
$this->num = mysql_num_rows($this->res);

if($this->num > 0){

$Glb_Top = (int)($Glb_Top + 1);
$Glb_Stack[$Glb_Top] = $this->get_child($this->res);
}
$this->sql = "SELECT MID FROM tbl_member WHERE SponserID = '$Glb_Ptr' AND Position = 'L'";
$this->res = mysql_query($this->sql)or die("2.Error in get_left_child function : ".mysql_error());
$this->num = mysql_num_rows($this->res);

if($this->num > 0){

$Glb_Ptr = $this->get_child($this->res);
$Glb_Node_Array[] = $Glb_Ptr;
}else{

$Glb_Ptr = $Glb_Stack[$Glb_Top];
$Glb_Top = (int)($Glb_Top - 1);
$Glb_Node_Array[] = $Glb_Ptr;
}
if($Glb_Ptr == NULL){

$Gbl_AllMemberCount = (int)(count(array_unique($Glb_Node_Array)));return;
}else{


$this->get_all_child_node();
}

}
}

//////////////////////////////////////////////////////////////////////////////////
//////////////////// End Common Class /////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////


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

//////////////THIS DB CLASS CREATED BY ZIYA///////////

/*
1) Functions Description :

(a) setup_conn() [This function used to establish a connection to database]
(b) db_connect() [This function is also used to establish a connection to database]
(c) select_db() [This function is used to select database]
(d) query() [This function is used to execute a query and get result from it]
(e) fetch_array() [This function is used to fetch an array of query result]
(f) num_rows() [This function is responsible for give num of rows]
(g) last_insertid() [This function is give last inserted id]
(h) insert_record() [This function is used to insert a record]
(i) update_record() [This function is used to select record]
(j) delete_record() [This function is used to delete records according to condition]
(k) select_record() [This function is used to select records according to OR condition]
(l) delete_all_record() [This function is used to delete all records from a table]
(m) select_all_record() [This function is used to select all records from a table]
(n) fetchSingleRow($result) [For fetch single record]
(o) executeQuery($query)
(p) select_records() [This function is used to select records according to AND condition]
*/
/***********************************************************************************************/

class db
{

var $hostname;// Hostname that the object should connect to

var $username;// Username that the object should use

var $password;// Password that the object should use

var $database;// Database that the object should use

var $query_num;// counts the total queries that the object has done.

var $con;//contains connection object.

function setup_conn($hostname,$username,$password)
{
$this->hostname = $hostname;
$this->username = $username;
$this->password = $password;
}

function db_connect()
{
$con = mysql_connect($this->hostname,$this->username,$this->password)or die(mysql_error());/*Open a non persistant connection to the server.*/
if (!$con)
{
echo 'Connection to database server at: '.$this->hostname.' failed.';
return false;
}
else
{
return $con;
}
}

function db_pconnect()
{
$result = mysql_pconnect($this->hostname,$this->username,$this->password);/*Open a persistant connection to the server. */

if (!$result)
{
echo 'Connection to database server at: '.$this->hostname.' failed.';
return false;
}
return $result;
}

function select_db($database,$con)
{
$this->database = $database;
$this->con=$con;
if (!mysql_select_db($this->database,$this->con))
{
'Selection of database: '.$this->database.' failed.';
return false;
}
}

function query($query)
{
$result = mysql_query($query) or die("Query failed:$query

" . mysql_error());
return $result;
}

function fetch_array($result)
{
return mysql_fetch_array($result);
}

function return_query_num()
{
return $this->query_num;
}

function num_rows($result)
{
return mysql_num_rows($result);
}

function last_insertid()
{
return mysql_insert_id();
}

function fetchSingleRow($result)
{
return mysql_fetch_row($result);
}

function delete_all_record($table)
{
$delete_query = "DELETE FROM $table";
$delete_result = mysql_query($delete_query)or mysql_error();

$flag = mysql_affected_rows();
if($flag != 0)
return true;
else
return false;
}

function select_all_record($table)
{
$select_query = "SELECT * FROM ".$table;
$select_result = mysql_query($select_query)or mysql_error();
return $select_result;
}

function select_record($conditon,$table)
{
$len = count($conditon);
$i = 0;
$cond = "";
$sql = "select * from ".$table." where ";

foreach ($conditon as $key => $value) {
if($len - 1 == $i)
$cond.= $key."='".$value."'";
else
$cond.= $key."='".$value."' and ";
$i++;
}

$sql = $sql.$cond;
return mysql_query($sql);
}

function get_row($rs){

$this->row = mysql_fetch_object($rs);
return $this->row;
}

function select_record_with_limit($conditon,$table,$limit)
{
$len = count($conditon);
$i = 0;
$cond = "";
$sql = "select * from ".$table." where ";

foreach ($conditon as $key => $value) {
if($len - 1 == $i)
$cond.= $key."='".$value."'";
else
$cond.= $key."='".$value."' and ";
$i++;
}

echo $sql = $sql.$cond." ".$limit;
return mysql_query($sql);
}

function insert_record($qry,$table)
{
$len = count($qry);
$i = 0;
$field = "";
$data = "";

foreach ($qry as $key => $value) {
if($len - 1 == $i)
{
$field.= $key;
$data.= "'".$value."'";
}
else
{
$field.= $key.",";
$data.= "'".$value."',";
}
$i++;
}

$sql = "insert into ".$table."(".$field.")values(".$data.")";
mysql_query($sql)or die(mysql_error());
//return $this -> last_insertid();
}


function update_record($sql,$conditon,$table)
{
$len = count($sql);
$i = 0;
$data = "";

foreach ($sql as $key => $value) {
if($len - 1 == $i)
$data.= $key."='".$value."'";
else
$data.= $key."='".$value."',";
$i++;
}

$lim = count($conditon);
$j = 0;
$cond = "";

foreach ($conditon as $key => $value) {
if($lim - 1 == $j)
$cond.= $key."='".$value."'";
else
$cond.= $key."='".$value."' and ";
$j++;
}

$sql = "update ".$table." set ".$data." where ".$cond;
mysql_query($sql);
return mysql_affected_rows();
}

function delete_record($conditon,$table)
{
$len = count($conditon);
$i = 0;
$cond = "";
$sql = "delete from ".$table." where ";

foreach ($conditon as $key => $value) {
if($len - 1 == $i)
$cond.= $key."='".$value."'";
else
$cond.= $key."='".$value."' and ";
$i++;
}

$sql = $sql.$cond;
mysql_query($sql);
return mysql_affected_rows();
}
}
$dbobj = new db();
?>

2 comments:

  1. Hi ziya I m thanking very very much. I am very pleaser that I got a binary tree logic.

    ReplyDelete
  2. This Code is nice But This has not run on my local there may be error in my database connection or else.

    Can you send me database and complete code archive

    on my mail my mail id is

    h.subhani@live.com

    I will always thankful.

    ReplyDelete