<?php
include_once "defines.php";



////////////////////////////////////////////////////////////
function db_fetch($settings){

//var_dump($settings);   
//// updated with  echo annd erros on 11/3/20

if (array_key_exists('table', $settings)) {
	$table=$settings['table'];}
else{
$table=mmh_table;	
}

if (array_key_exists('id_col', $settings)) {
	$id_col=$settings['id_col'];}
else{
$id_col=id_col;	
}

if (array_key_exists('status_col', $settings)) {
	$status_col=$settings['status_col'];}
else{
$status_col=status_col;	
}

if (array_key_exists('ref_col', $settings)) {
	$ref_col=$settings['ref_col'];}
else{
$ref_col=ref;	
}

if (array_key_exists('status_value', $settings)) {
	$status_value=$settings['status_value'];}
else{
$status_value="Active";	
}

if (array_key_exists('status_value', $settings)) {
	$status_value=$settings['status_value'];}
else{
$status_value="Active";	
}

if (array_key_exists('order_col', $settings)) {
	$order_col=$settings['order_col'];}
else{
$order_col=id_col;
}
	
if (array_key_exists('fields_array', $settings)) {
	$fields_array=$settings['fields_array'];}    

if (array_key_exists('where_col', $settings)) {
	$where_col=$settings['where_col'];}

if (array_key_exists('where_value', $settings)) {
	$where_value=$settings['where_value'];}
    
if (array_key_exists('where_col2', $settings)) {
	$where_col2=$settings['where_col2'];}

if (array_key_exists('where_value2', $settings)) {
	$where_value2=$settings['where_value2'];}

if (array_key_exists('sec_id_col', $settings)) {
	$sec_id_col=$settings['sec_id_col'];}    

if (array_key_exists('id', $settings)) {
	$id=$settings['id'];}    

if (array_key_exists('to_find', $settings)) {
	$to_find=$settings['to_find'];}    

if (array_key_exists('ref_value', $settings)) {
	$ref_value=$settings['ref_value'];}    

if (array_key_exists('sec_id', $settings)) {
	$sec_id=$settings['sec_id'];}    
	
if (array_key_exists('limit', $settings)) {
	$limit=$settings['limit'];}    
	
if (array_key_exists('order', $settings)) {
	$order=$settings['order'];}
    
if (array_key_exists('counter', $settings)) {
	$counter=$settings['counter'];}    

if (array_key_exists('type', $settings)) {
	$type=$settings['type'];}    

if (array_key_exists('select_all_pagination_start', $settings)) {
   $select_all_pagination_start=$settings['select_all_pagination_start'];
   }	
if (array_key_exists('select_all_pagination_per_page_limit', $settings)) {
   $select_all_pagination_limit=$settings['select_all_pagination_per_page_limit'];
   }		
if (array_key_exists('keywords', $settings)) {
   $keywords=$settings['keywords'];
    }
    
if (array_key_exists('errors', $settings)) {
   $error=$settings['errors'];
  }		
if (array_key_exists('echo', $settings)) {
   $echo=$settings['echo'];
  }		
if (array_key_exists('uplink', $settings)) {
   $uplink=$settings['uplink'];
  }		
else{
$uplink=uplink();	
}    


$link= $uplink;


if(isset($type) && $type=="parameter_old"){
$query="SELECT ".implode(",", $fields_array)." from $table WHERE $status_col='$status_value' AND $ref_col IN($ref_value) LIMIT 1";
}

if(isset($type) && $type=="if_exists"){
//select all yes ref yes sec_id	
$query="SELECT $where_col from $table WHERE $where_col IN($where_value) AND $status_col='$status_value'";
}


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

if(isset($type) && $type=="0"){
$query="SELECT $to_find from $table WHERE $id_col='$id'";
//echo $query;
}


if(isset($type) && $type=="status_ref"){
//select all yes status	
$query="SELECT ".implode(",", $fields_array)." from $table WHERE $status_col='$status_value' AND $ref_col IN($ref_value)";
}

if(isset($type) && $type=="id_status_ref"){
//select all yes status	
$query="SELECT ".implode(",", $fields_array)." from $table WHERE $id_col='$id' AND $status_col='$status_value' AND $ref_col IN($ref_value)";
}

if(isset($type) && $type=="wherecol_ref_status"){
//select all updates
$query="SELECT ".implode(",", $fields_array)." from $table WHERE $where_col='$where_value' AND $ref_col IN($ref_value) AND $status_col='$status_value'";
}

if(isset($type) && $type=="wherecol1_wherecol2_ref_status"){
//select all updates
$query="SELECT ".implode(",", $fields_array)." from $table WHERE $where_col='$where_value' AND $where_col2='$where_value2' AND $ref_col IN($ref_value) AND $status_col='$status_value'";
}

//////////////////////////////////////////////////////////////////////////////
 if(isset($type) && $type=="view2"){
$query="SELECT ".implode(",", $fields_array)." from $table WHERE $status_col='$status_value'
 AND $where_col='$where_value' AND $ref_col IN($ref_value)";
 }

 if(isset($type) && $type=="updates"){
//select all yes array by legend order
$query="SELECT ".implode(",", $fields_array)." from $table WHERE $status_col='$status_value'
 AND $where_col='$where_value' AND $ref_col IN($ref_value)";
 }
/////////////////////////////////////////////////////////////////////////////////////

if (array_key_exists('order_col', $settings) && (array_key_exists('order', $settings))) {
$order_added="ORDER BY ".$order_col." ".$order;
$query=$query.$order_added;
}


if (array_key_exists('select_all_pagination_start', $settings) && (array_key_exists('select_all_pagination_per_page_limit', $settings))) {
$page_added="LIMIT ".$select_all_pagination_start.", ".$select_all_pagination_limit;
$query=$query.$page_added;
}

if (array_key_exists('limit', $settings) && (array_key_exists('limit', $settings))) {
$limit_added=" LIMIT ".$limit." ";
$query=$query.$limit_added;
}


if(isset($type) && $type=="like_ref_status"){
//select all updates
$cols = array('0'); // Stop errors when $words is empty
foreach($cols as $col){
    $sql[] = $col.'LIKE %'.$term.'%';
}
$query = 'SELECT * FROM users WHERE '.implode(" OR ", $sql);
}


if(isset($type) && $type=="0"){
$query="SELECT $to_find from $table WHERE $id_col='$id'";
//echo $query;
}

if(isset($echo)){
if($echo!="0"){	
echo "<h4>type is : ".$type." </h4><br />";
echo "<br /> STMT IS. <br />".$query."<br />";	
}
}


if(isset($type) && $type=="0"){
//select to_find
$select_query=$link->query($query);
if($select_query){
while($fetch_select = $select_query->fetch_array()){
 	 $result= $fetch_select[$to_find];
    return $result;



// Frees the memory associated with a result
$fetch_select->free();

// close connection 
$link->close();

}
}
else{
	
echo "No Query Selected 243"."<br />".$query."<br />".$type;	
}

}

elseif(isset($counter)) {
//		echo $query;

$select_query=$link->query($query,MYSQLI_STORE_RESULT);	
if($select_query){
//echo $query; 
$deal_count=mysqli_num_rows($select_query);
//echo $deal_count;
return $deal_count;
// close connection 
$link->close();
}
else{
if(isset($error)){
if($error!="0"){
echo "Error no Query Performed 259<br />";
echo "TABLE : -".$table."- DB Error:: ".mysqli_error($link).":: i2d Technologies - Website Engine (c)2015";
echo "<br /><br /><br /> STMT :".$query;    
}
}	
$deal_count=0;
return $deal_count;	
}    
	
}

else{
if($select_query=$link->query($query)) {	
if($select_query->num_rows > 0){
while($fetch_select = $select_query->fetch_array()){
$result[] = $fetch_select;
    }
	if($select_query->num_rows > 0){ 
    return $result;}

// Frees the memory associated with a result
$fetch_select->free();

// close connection 
$link->close();
}
else{
if(isset($error)){
if($error!="0"){
echo "Error no Query Performed 260<br />";
echo "TABLE : -".$table."- DB Error:: ".mysqli_error($link).":: i2d Technologies - Website Engine (c)2015";
echo "<br /><br /><br /> STMT :".$query;    
}
}	

}
}
else{
if(isset($error)){
if($error!="0"){
echo "Error no Query Performed 261<br />";
echo "TABLE : -".$table."- DB Error:: ".mysqli_error($link).":: i2d Technologies - Website Engine (c)2015";
echo "<br /><br /><br /> STMT :".$query;    
}
}	

}
} ////else ends
///////////////////////////////////////////////////////////////////

}

///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
function db_ref($remote_tbl,$remote_col,$thisid,$tofind,$echo1=0,$uplink=0){
if(isset($uplink) && $uplink!=0){
$link=$uplink;
}
else{
$link=uplink();
}
//$link= uplink();

$select_query=$link->query("SELECT $tofind from $remote_tbl WHERE $remote_col='$thisid'");

if(isset($echo1) && $echo1!=0){
    echo "SELECT $tofind from $remote_tbl WHERE $remote_col='$thisid'";
}

if($select_query){
while($fetch_select = $select_query->fetch_array()){
 	 $result= $fetch_select[$tofind];
    return $result;


// Frees the memory associated with a result
$fetch_select->free();

// close connection 
$link->close();

}
}
}

///////////////////////////////////////////////////////////////////////////////////
function db_editdata($table, $idcol, $form_data, $id, $alias = 0, $uplink = null) {
   // echo "Reached Here<br>";
    
    // Get database connection
    if (isset($uplink) && is_object($uplink) && $uplink instanceof mysqli) {
        $link = $uplink;
        $should_close = false;
    } else {
        $link = uplink();
  //      echo "Reached Else uplink<br>";
        $should_close = true;
    }
    
    // Validate inputs with better logic
    if (empty($table) || empty($idcol) || !is_array($form_data) || $id === null || $id === '') {
        if ($should_close) $link->close();
   //     echo "Reached Validation - Input validation failed<br>";
   //     echo "DEBUG - Validation details:<br>";
   //     echo "Table: '" . (isset($table) ? $table : 'NULL') . "'<br>";
   // //    echo "ID Column: '" . (isset($idcol) ? $idcol : 'NULL') . "'<br>";
   //     echo "Form Data is array: " . (is_array($form_data) ? 'YES' : 'NO') . "<br>";
   //     echo "ID Value: '" . (isset($id) ? $id : 'NULL') . "' (Type: " . gettype($id) . ")<br>";
        return false;
    }
    
    // Clean input data - trim whitespace
    $id = trim($id);
    if ($alias !== 0 && $alias !== null && $alias !== '') {
        $alias = trim($alias);
    }
    
 //   echo "DEBUG - Table: $table, ID Column: $idcol, ID Value: '$id', Alias: '$alias'<br>";
    
    // Build WHERE clause
    if ($alias !== 0 && $alias !== null && $alias !== '') {
        $whereSQL = " WHERE `" . mysqli_real_escape_string($link, $idcol) . "` = '" . mysqli_real_escape_string($link, $id) . "' AND `alias` = '" . mysqli_real_escape_string($link, $alias) . "'";
        echo "Reached has Alias<br>";
    } else {
        $whereSQL = " WHERE `" . mysqli_real_escape_string($link, $idcol) . "` = '" . mysqli_real_escape_string($link, $id) . "'";
        echo "Reached Has No alias<br>";
    }
    
    // Escape table name
    $table_escaped = mysqli_real_escape_string($link, $table);
    
    // Build SET clause with properly escaped values
    $sets = array();
    foreach ($form_data as $column => $value) {
        $column_escaped = mysqli_real_escape_string($link, $column);
        $value_escaped = mysqli_real_escape_string($link, trim($value)); // Trim values too
        $sets[] = "`$column_escaped` = '$value_escaped'";
    }
//    echo "Reached Mysqli Escaped Loop End<br>";
    
    // Build complete SQL
    $sql = "UPDATE `$table_escaped` SET " . implode(', ', $sets) . $whereSQL;
    
    // Debug: Show the SQL being executed
//    echo "<br><strong>DEBUG - UPDATE SQL:</strong><br>" . htmlspecialchars($sql) . "<br><br>";
    
    // FIRST: Check if record exists with SELECT query
    $select_sql = "SELECT * FROM `$table_escaped` $whereSQL";
//    echo "<br><strong>DEBUG - SELECT SQL (to verify record exists):</strong><br>" . htmlspecialchars($select_sql) . "<br>";
    
    $select_result = mysqli_query($link, $select_sql);
    if ($select_result) {
        $row_count = mysqli_num_rows($select_result);
 //       echo "<br><strong>DEBUG - Matching rows found:</strong> $row_count<br>";
        
        if ($row_count > 0) {
      //      echo "<strong>Found matching record(s):</strong><br>";
            while ($row = mysqli_fetch_assoc($select_result)) {
   //             echo "<pre>";
                print_r($row);
         //       echo "</pre>";
            }
            mysqli_free_result($select_result);
        } else {
      //      echo "<strong>WARNING: No matching records found!</strong><br>";
      //      echo "<strong>This means your WHERE clause doesn't match any existing records.</strong><br>";
     //       echo "<strong>Check your ID value and alias value.</strong><br>";
            
            if ($should_close) {
                $link->close();
            }
            return false;
        }
    } else {
  //      echo "<strong>ERROR in SELECT query:</strong> " . mysqli_error($link) . "<br>";
        if ($should_close) {
            $link->close();
        }
        return false;
    }
    
    // Execute UPDATE query only if record exists
    $result = mysqli_query($link, $sql);
    
    if ($result) {
        // Check how many rows were actually affected
        $affected_rows = mysqli_affected_rows($link);
 //       echo "<br><strong>DEBUG - Rows affected by UPDATE:</strong> $affected_rows<br>";
        
        if ($affected_rows > 0) {
            echo "<br><strong>SUCCESS: Record updated successfully!</strong><br>";
        } else {
            echo "<br><strong>WARNING: No rows were updated (possibly no changes to make)</strong><br>";
        }
        
        if ($should_close) {
            $link->close();
        }
        
        return $result;
    } else {
        // Error handling
        $error_msg = "TABLE: -$table- DB Error: " . mysqli_error($link) . " :: i2d Technologies - Website Engine (c)2015";
        $error_msg .= "<br><br>STMT: " . htmlspecialchars($sql);
        
        if ($should_close) {
            $link->close();
        }
        
        echo $error_msg;
        exit();
    }
}
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
function db_editdata_new($table, $idcol, $form_data, $id, $alias = 0, $uplink = null) {
    // Get database connection
    if (isset($uplink) && is_object($uplink) && $uplink instanceof mysqli) {
        $link = $uplink;
        $should_close = false; // Don't close connection we didn't create
    } else {
        $link = uplink();
        $should_close = true; // Close connection we created
    }
    
    // Validate inputs
    if (empty($table) || empty($idcol) || !is_array($form_data) || empty($id)) {
        if ($should_close) $link->close();
        return false;
    }
    
    // Escape table and column names
    $table_escaped = mysqli_real_escape_string($link, $table);
    $idcol_escaped = mysqli_real_escape_string($link, $idcol);
    $id_escaped = mysqli_real_escape_string($link, $id);
    
    // Build WHERE clause
    if ($alias != 0) {
        $alias_escaped = mysqli_real_escape_string($link, $alias);
        $whereSQL = " WHERE `$idcol_escaped` = '$id_escaped' AND `alias` = '$alias_escaped'";
    } else {
        $whereSQL = " WHERE `$idcol_escaped` = '$id_escaped'";
    }
    
    // Build SET clause with proper escaping
    $sets = array();
    foreach ($form_data as $column => $value) {
        $column_escaped = mysqli_real_escape_string($link, $column);
        $value_escaped = mysqli_real_escape_string($link, (string)$value);
        $sets[] = "`$column_escaped` = '$value_escaped'";
    }
    
    // Build final SQL
    $sql = "UPDATE `$table_escaped` SET " . implode(', ', $sets) . $whereSQL;
    
    // Debug: Show the SQL being executed (remove after debugging)
    echo "<br><strong>DEBUG - UPDATE SQL Query:</strong><br>" . htmlspecialchars($sql) . "<br><br>";
    
    // Execute query
    $result = mysqli_query($link, $sql);
    
    if ($result) {
        $affected_rows = mysqli_affected_rows($link);
        
        // Close connection if we created it
        if ($should_close) {
            $link->close();
        }
        
        return $affected_rows; // Return number of affected rows
    } else {
        // Error handling
        $error_msg = "TABLE: -$table- DB Error: " . mysqli_error($link) . " :: i2d Technologies - Website Engine (c)2015";
        $error_msg .= "<br><br><br>STMT: " . htmlspecialchars($sql);
        
        // Close connection if we created it
        if ($should_close) {
            $link->close();
        }
        
        echo $error_msg;
        exit();
    }
}
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
function db_editdata_old($table,$idcol,$form_data,$id,$alias=0,$uplink=0){

if(isset($uplink)&&$uplink!=0){
$link=$uplink;
}
else{
$link= uplink();
}


//$link= uplink();
$ref=ref;
//$whereSQL ="WHERE"." ".$idcol." "."="." ".$id;
if($alias!=0){
$whereSQL ="  "."WHERE"."  ".$idcol." = ".$id." AND ".$ref." = '".$alias."'";
}
else{
$whereSQL ="  "."WHERE"."  ".$idcol." = '".$id."' ";	
}
	
	$fields = array_keys( $form_data );  
    $values=array_map(array($link, 'real_escape_string'), array_values( $form_data ) );
	
    $sql = "UPDATE ".$table." SET ";
    // loop and build the column /
    $sets = array();
    foreach($form_data as $column => $value)
    {
         $sets[] = "`".$column."` = '".$value."'";
    }
    $sql .= implode(', ', $sets);
    // append the where statement
    $sql .= $whereSQL;

	//echo $sql;
	
	return  $link->query($sql);

	
/// Part 2 Starts

   if(!$sql){
	   echo mysqli_error($link);
       echo "TABLE : -".$table."- DB Error:: ".mysqli_error($link).":: i2d Technologies - Website Engine (c)2015";
	   exit();
	   
   }

	return  $sql;

// close connection 
$link->close();


}

//////////////////////////////////////////////////////////////////////////////
// Helper function to remove empty values from arrays (optional)
function filter_empty_values($array) {
    return array_filter($array, function($value) {
        return $value !== '' && $value !== null;
    });
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// Fixed db_insertdata function
function db_insertdata($table, $arrayname, $arrayname2, $switch, $uplink = null) {
    // Get database connection - check if uplink is actually a mysqli object
    if (isset($uplink) && is_object($uplink) && $uplink instanceof mysqli) {
        $link = $uplink;
        $should_close = false; // Don't close connection we didn't create
    } else {
        $link = uplink();
        $should_close = true; // Close connection we created
    }
    
    // Validate inputs
    if (empty($table) || !is_array($arrayname) || !is_array($arrayname2)) {
        if ($should_close) $link->close();
        return false;
    }
    
    // Process first array
    $fields = array_keys($arrayname);
    if ($switch == 1) {
        // Base64 encode values first, then escape
        $values1 = array_map("base64_encode", array_values($arrayname));
        $values = array();
        foreach ($values1 as $val) {
            $values[] = mysqli_real_escape_string($link, (string)$val);
        }
    } else {
        // Just escape values
        $values = array();
        foreach (array_values($arrayname) as $val) {
            $values[] = mysqli_real_escape_string($link, (string)$val);
        }
    }
    
    // Process second array
    $fields2 = array_keys($arrayname2);
    $values2 = array();
    foreach (array_values($arrayname2) as $val) {
        $values2[] = mysqli_real_escape_string($link, (string)$val);
    }
    
    // Merge fields and values
    $fields3 = array_merge($fields, $fields2);
    $values3 = array_merge($values, $values2);
    
    // Escape table name for security
    $table_escaped = mysqli_real_escape_string($link, $table);
    
    // Build SQL with proper escaping
    $fields_str = implode("`,`", $fields3);
    $sql = "INSERT INTO `$table_escaped` (`$fields_str`) VALUES ('" . implode("','", $values3) . "')";
    
    // Execute query
    $query = mysqli_query($link, $sql);
    
    if ($query) {
        $last_id = mysqli_insert_id($link);
        
        // Close connection if we created it
        if ($should_close) {
            $link->close();
        }
        
        return $last_id;
    } else {
        // Error handling
        $error_msg = "TABLE: -$table- DB Error: " . mysqli_error($link) . " :: i2d Technologies - Website Engine (c)2015";
        $error_msg .= "<br><br><br>STMT: " . htmlspecialchars($sql);
        
        // Close connection if we created it
        if ($should_close) {
            $link->close();
        }
        
        echo $error_msg;
        exit();
    }
}
//////////////////////////////////////////////////////////////////////////////
function db_insertdata_old($table,$arrayname,$arrayname2,$switch,$uplink=0){

if(isset($uplink)&& $uplink!=0){
$link=$uplink;
}
else{
$link= uplink();
}

//$link= uplink();
//$escaped_row = array_map(array($link, 'real_escape_string'), $row);

    if($switch==1){
	$fields = array_keys( $arrayname );  
   	$values1 = array_map("base64_encode", array_values( $arrayname ) );
	$values=array_map(array($link, 'real_escape_string'), array_values( $values1 ));
	//$values=array_map("mysql_real_escape_string", array_values( $values1 ) );
	$fields2 = array_keys( $arrayname2 );  
	$values2=array_map(array($link, 'real_escape_string'), array_values( $arrayname2 ) );
	}
	if($switch==0){
	$fields = array_keys( $arrayname );  
   	$values=array_map(array($link, 'real_escape_string'), array_values( $arrayname ) );
	$fields2 = array_keys( $arrayname2 );  
	$values2=array_map(array($link, 'real_escape_string'), array_values( $arrayname2 ) );
	}
	$fields3= array_merge($fields, $fields2);
	$values3=array_merge($values, $values2);
	
	$sql=( "INSERT INTO $table (".implode(",",$fields3).") VALUES ('".implode("','", $values3 )."');");
	
	
	
	$query=mysqli_query($link, $sql);
    $last_id = mysqli_insert_id($link);
    
/// Part 2 Starts
if($query){
	   return $last_id;
   }
   else{
	
    echo "TABLE : -".$table."- DB Error:: ".mysqli_error($link).":: i2d Technologies - Website Engine (c)2015";
echo "<br /><br /><br /> STMT :".$sql;
	
exit();	
   }
   //log_error($query,"1");

//return  $last_id;


// close connection 
$link->close();

}

//////////////////////////// function ends ///////////////////////////////////
////////////////////////// Common "Addons" Start ////////////////////////////////////
////////////////////////// Common "Addons" Start ////////////////////////////////////
////////////////////// CORE FUNCTION | MODULE FUNCTION (c) i2d Technologies //////////////////////////////////
if (!function_exists('client_notify')) {
function client_notify($table,$subject,$dated,$record_id) { 

    $list=
	array
	(
'booking'=>
array('class'=>'bg-primary','icon'=>'fa-info-circle','link'=>'booking','title'=>'A New Booking is Made!','description'=>'Click to view booking details'),
'system'=>
array('class'=>'bg-primary','icon'=>'fa-info-circle','link'=>'booking','title'=>'A New Booking is Made!','description'=>'Click to view booking details'),
	);

$class=$list[$subject]['class'];
$icon=$list[$subject]['icon'];
$link=$list[$subject]['link'];
$title=$list[$subject]['title'];
$description=$list[$subject]['description'];


$notify_array=array(
client_notification_table_title=>$title,
client_notification_table_icon=>$icon,
client_notification_table_link=>$link,
client_notification_table_class=>$class,
client_notification_table_dated=>$dated,
client_notification_table_read=>"unread",
client_notification_table_ref=>client_notification_table_default_ref,
client_notification_table_status=>"Active",
client_notification_table_record_id=>$record_id,
client_notification_table_description=>$description,
);

$array2=array();

db_insertdata($table,$array2,$notify_array,0);

return;

} 
}
/////////////////////////// function ends /////////////////////////////////
////////////////////// CORE FUNCTION | MODULE FUNCTION (c) i2d Technologies //////////////////////////////////
if (!function_exists('if_exists')) {
function if_exists($id,$table) { 


$fields=array(id_col);

$settings2=array(
    'table'=>$table,
    'counter'=>'1',
    'fields_array'=>$fields,
    'where_col'=>id_col,
    'where_value'=>$id,
    'status_col'=>status,
    'status_value'=>'Active',	
    'order'=>'ASC',
    'type'=>'if_exists');	
    $deal_count=db_fetch($settings2);


return  $deal_count;
 
} 
}
/////////////////////////// function ends /////////////////////////////////
////////////////////// CORE FUNCTION | MODULE FUNCTION (c) i2d Technologies //////////////////////////////////
if (!function_exists('stats2')) {
function stats2($table,$ref,$col,$col_id) { 

/////////////////////////// function ends /////////////////////////////////

$link=uplink();
$rt="SELECT id FROM $table where alias='$ref' AND $col='$col_id'";
$table_query=$link->query($rt);
//echo $rt;
if($table_query){
$deal_count=mysqli_num_rows($table_query);
return $deal_count;
// close connection 
$link->close();
}
else{
$deal_count=0;
return $deal_count;	
}

}
}
//////////////////////////// function ends ///////////////////////////////////



?>