/home/mip/public_html_/old-website/func_select.php
<?
 /* The collection of functions that query the database in select statement. */
require_once("config.php");

	function blockfileupload() {
	global $diskspace;
	$diskusageid = getid("menu_low_id","menu_low","name","Disk Usage");
	$diskusage_id=getfeature("status","feature_module","id","".$diskusageid."","tablename","menu_low");
		if($diskusage_id) {
			require_once("diskspaceblock.php");
			$remove = array("G","GB","M","MB");
			if(strstr(showdiskspace(),"M")) {
				$diskusage = trim(substr(str_replace($remove,"",showdiskspace()),0,-1))/1024;
			} else {
				$diskusage = trim(substr(str_replace($remove,"",showdiskspace()),0,-1));
			}
			if($diskspace>$diskusage) {
				return 1;
			} else {
				return;
			}
		} else {
			return 1;
		}	
	}
	/* create a round off number */
	function cmd_guid(){
	global $SERVER_ADDR; 
	$loc_guid = strtoupper(md5(uniqid(rand(),1)));
	return ($loc_guid);
	}
	/* The function getWEcount() get the count of work experiance */
	function getEDcount($table,$applicant_id){
    $db = db_connect();
    if (!$db){
      echo "Error connecting to database.</center>\n";
	    exit;
    }
		
		$query = "select count(*) from $table where applicant_id=$applicant_id";
		$result = mysql_query($query,$db);
		$myrow = mysql_fetch_array($result);
		return $myrow[0];
	}// end of function getWEcount   
	/* Function to check if the visitor is a valid IRIS user. */
	
	/* The function getWEcount() get the count of work experiance */
	function getWEcount($table,$applicant_id){
    $db = db_connect();
    if (!$db){
      echo "Error connecting to database.</center>\n";
	    exit;
    }
		
		$query = "select count(*) from $table where applicant_id=$applicant_id";
		$result = mysql_query($query,$db);
		$myrow = mysql_fetch_array($result);
		return $myrow[0];
	}// end of function getWEcount


	/* Function to check if the visitor is a valid IRIS user. */
	function login($username,$password){
		
	$db = db_connect();
	if (!$db){
		 echo "Can't connect to the MySQL database...";
		 exit;
	}	 

	$query = "Select applicant_id,position_id1 from web_personal where email='$username' and password='$password'";
	$result = mysql_query($query,$db);
	
	$query1 = "Select applicant_id,position_id1 from personal where email='$username' and password='$password'";
	$result1 = mysql_query($query1,$db);
	
	if($result) {
		$myrow = mysql_fetch_array($result);					
		if (!$result) return 0;
		elseif (mysql_num_rows($result)>0) return "web_personal";
		//else return 0;
	}	
	
	if($result1) {
		$myrow = mysql_fetch_array($result1);					
		if (!$result1) return 0;
		elseif (mysql_num_rows($result1)>0) return "personal";
		//else return 0;
	}	
	
	}
   
	function next_back($total,$range,$page=1,$url){
		if(!$total)return true;
		if(!$page)$page=1;
		$back=$page-1;
		$next=$page+1;
		$pages=ceil($total/$range);// how many pages?
		
		//$input=" page:<input type=text name=page value=$page size=2 onfocus='select();'><input type=submit value='Go'> of $pages";
	  $input=" page: $page of $pages";
			
		$linker.=" <form action='$url'>";	
		if($page!=1&&$page!=$pages)$linker.="<a href='$url&page=1'>< First </a> | <a href='$url&page=$back'>Back</a> |$input | <a href='$url&page=$next'>Next</a> | <a href='$url&page=$pages'> Last > </a>";
		elseif($page==1)$linker.="$input <a href='$url&page=$next'>Next</a> | <a href='$url&page=$pages'>  Last > </a>";
		elseif($page==$pages)$linker.="<a href='$url&page=1'>< First </a> | <a href='$url&page=$back'>Back</a> $input";	
		$linker.=" </form>";		
		if($pages==1)$linker="";
	  $start=(($page-1)*$range);//where to start?
		return array($start,$linker);
	}


  /* The function getdata() get all the data in a table. */
	function getdata($query){
    $db = db_connect();
    if (!$db){
      echo "Error connecting to database.</center>\n";
	    exit;
    }
		$count = 1;
    @$result = mysql_query($query, $db);
		@$num_rows = mysql_num_rows($result);

    if ($num_rows > 0)
		{
	  while ($myrow = mysql_fetch_array($result)){
		   foreach($myrow as $key => $value){
			    if (!is_numeric($key)){
		 		   $row[$key]=stripslashes($value);
			 		}
		   }
		$array1[$count] = $myrow;
		$count++;
		}
		return $array1;
		}
		else return $array1;
		
		
	}// end of function getdata
	
 // select one field only
	function getdata_one($select,$table,$field,$value)
 { 		 
  //extract from the database all users
  if (!($conn = db_connect()))
    return false;
	$sql = "select $select from $table where $field = ";
	if (is_numeric($value)){$sql .= " $value";}
	else {$sql .= " '$value'";}
  $result = mysql_query($sql);
  if (!$result || (mysql_num_rows($result)<=0))
    return false;	
  $ans =  mysql_fetch_array ($result);
      foreach ($ans as $key => $value)      {
 			   $ans[$key]=stripslashes($value);
      } 
	return $ans;
}
 
	/* The function getname() get the equivalent name of an id */
	function getname($id,$table,$field){
    $db = db_connect();
    if (!$db){
      echo "Error connecting to database.</center>\n";
	    exit;
    }
		
		$query = "select name from $table where $field=$id";
		$result = mysql_query($query,$db);
		$myrow = mysql_fetch_array($result);
		return $myrow[0];
	}// end of function getname
	
	/* The function getname() get the equivalent name of an id */
	function getfield($what,$table,$field,$id){
    $db = db_connect();
    if (!$db){
      echo "Error connecting to database.</center>\n";
	    exit;
    }
		
		$query = "select $what from $table where $field='$id'";
		$result = mysql_query($query,$db);
		$myrow = mysql_fetch_array($result);
		return $myrow[0];
	}// end of function getname
	
	/* The function get_applicantname() get the equivalent name of an applicant_id */
	function get_applicantname($applicant_id){
    $db = db_connect();
    if (!$db){
      echo "Error connecting to database.</center>\n";
	    exit;
    }
		
		$query = "select (concat(lname,', ',fname,' ',mname)) as applicant_name from personal where applicant_id='$applicant_id'";
		$result = mysql_query($query,$db);
		$myrow = mysql_fetch_array($result);
		return $myrow[0];
	}// end of function get_applicantname
	
	
	function get_max_id()
	{
	$db = db_connect();
	$sql = "select max(applicant_id) from personal ";
	$result = mysql_query($sql, $db);
	$row = mysql_fetch_array ($result);
	if($row[0])  return $row[0];
	else  return 0;			
	}
  
	// function to get the applicant id
	function get_applicant_id(){
	      $max_id = get_max_id();
		 		$year = substr($max_id, 0, 4);
				$this_year = date("Y");

				if ($year != $this_year)	 $applicant_id = $this_year."000001"; 
				else{ $applicant_id=$max_id + 1;}		 		
				
				return($applicant_id);
	}

	function get_balance($applicant_id="")
	{
	  $db = db_connect();
	  
		/** payment **/
		$payment = getdata("select payment_id,total_amount from payment where applicant_id='$applicant_id'");
		$num_debits = count($payment);										
		
		for($i=1;$i<=$num_debits;$i++){
		   $payment_id = $payment[$i][payment_id];

			 $payment_detail = getdata("select SUM(amount) as total_paid from payment_detail where payment_id=$payment_id GROUP by payment_id");						
			 $balance = $payment[$i][total_amount] - $payment_detail[1][total_paid];							 
		   
			 $bal = $bal + $balance;
			 $total_bill = $total_bill + $payment[$i][total_amount];
			 $total_paid = $total_paid + $payment_detail[1][total_paid];
		}
								
		$bill = array("total_bill"=>"$total_bill","total_paid"=>"$total_paid","balance"=>"$bal");																
		/** end of payment **/
						
	  if(empty($bill)) return 0;
	  else   return $bill;			
	}// end of function get_balance
	
	
	function check_if_full($jopos_id){
				$jop = getdata("select no_of_male as m,no_of_female as f,no_of_coed as c from jo_position where jo_pos_id='$jopos_id'");
        $num = getdata("select COUNT(jo_pos_id) AS n from process where jo_pos_id='$jopos_id'");
				$tot_jop = $jop[1][m]+$jop[1][f]+$jop[1][c];
				$available = $tot_jop - $num[1][n];
				if($available >= 1 ) return(0);
				else return(1);
	} 							  

	/*  Function to connect to the database */
	function db_connect(){
					
					global $db_host ,$db_name, $db_username, $db_password;

					$result = @mysql_pconnect("$db_host", "$db_username","$db_password");
					if (!$result) return false;
					if (!@mysql_select_db("$db_name")) return false;
					return($result);
					 					
	}
	
	
	/* Function to check if the visitor is a valid IRIS user. */
	function login_($username,$password){
		
		$db = db_connect();
		if (!$db){
			 echo "Can't connect to the MySQL database...";
		 	 exit;
	  }	 
		$query = "SELECT access_id FROM users WHERE username='$username' AND password=password('$password') ";
		$result = mysql_query($query,$db);
		$myrow = mysql_fetch_array($result);
		
		if($result) return $myrow[0];
    else return 0;		
	}				
  
		function optimize_tables(){
    $db = db_connect();
		if (!$db){
			 echo "Can't connect to the MySQL database...";
		 	 exit;
	  }	 
   
	  mysql_query("OPTIMIZE TABLE brokers",$db);
		mysql_query("OPTIMIZE TABLE clinics",$db);		
		mysql_query("OPTIMIZE TABLE complaints",$db);
		mysql_query("OPTIMIZE TABLE complaints",$db);
		mysql_query("OPTIMIZE TABLE country",$db);
		mysql_query("OPTIMIZE TABLE education",$db);
		mysql_query("OPTIMIZE TABLE employment",$db);
		mysql_query("OPTIMIZE TABLE family",$db);
		mysql_query("OPTIMIZE TABLE jo_position",$db);
		mysql_query("OPTIMIZE TABLE job_order",$db);
		mysql_query("OPTIMIZE TABLE medical",$db);
		mysql_query("OPTIMIZE TABLE payment",$db);
		mysql_query("OPTIMIZE TABLE payment_detail",$db);
		mysql_query("OPTIMIZE TABLE personal",$db);
		mysql_query("OPTIMIZE TABLE positions",$db);
		mysql_query("OPTIMIZE TABLE prequalify",$db);
		mysql_query("OPTIMIZE TABLE principals",$db);
		mysql_query("OPTIMIZE TABLE process",$db);
		mysql_query("OPTIMIZE TABLE training",$db);
		mysql_query("OPTIMIZE TABLE training_center",$db);		
		mysql_query("OPTIMIZE TABLE users",$db);
		
	  return 1;		
  }			

function get_yrs_exp($applicant_id) 
{ 

$db = db_connect();
if (!$db){
			 echo "Can't connect to the MySQL database...";
		 	 exit;
	  }	 
$employment = getdata("SELECT to_date,from_date																			
				 							 FROM  employment
				 							 WHERE applicant_id = '$applicant_id'");
$num_emp = count($employment);

for($i=1;$i<=$num_emp;$i++){		  

	if($employment[$i][from_date]!='0000-00-00' && $employment[$i][to_date]=='0000-00-00')
	  $employment[$i][to_date] = date(Y-m-d);
																					 
    $yrs = ( strtotime ( $employment[$i][to_date] ) - strtotime ( $employment[$i][from_date] ) ) / (86400 * 365);
	  $exp_yrs = $exp_yrs + $yrs;
	
}

$exp_yrs = number_format($exp_yrs,1);

if($exp_yrs!=0.0) return $exp_yrs;
else return; 
}

function do_form_row($position="&nbsp;",$number="&nbsp;",$description="&nbsp;")
{
	if($concat==""||$concat2==""||$concat3=="") 
	return; 
	else {
		$row  ="<tr>
              <td valign=top>".$position."</td>
              <td align=center valign=top>".$number."</td>
              <td>".$description."</td>
                </tr>";
	   return $row;
	 			}
}
?>