<?php 
##################################################################
#  This is the basic that can be called  within the web application
#  Include this after the configuration file on every page
#  File name: functions.php
##################################################################

//Call the configuration file
include('configuration.php');


	//function get_user()
//	{
//		$user_id = $_SESSION['id'];
//		return $user_id;
//	}





//not functional
//Get Current User's ID fro sessionif i need t use this in startic manner
	
	//function get user id brings user id 
	function get_user_id()
	{
		//collect the id using cookie
		$user_id = $_COOKIE['listd'];
		return $user_id;
	}



//Function to fetch user's Rool
	function get_user_role($user_id)
	{
		$result = mysql_query("SELECT * FROM users WHERE user_id = '$user_id'"); 
		$row = mysql_fetch_assoc($result);
		$role_id = $row['role_id'];

	return $role_id;
	}

//Function to fetch User Last IP
	function get_last_login_ip($user_id)
	{
		$result = mysql_query("SELECT * FROM users WHERE user_id = '$user_id'"); 
		$row = mysql_fetch_assoc($result);
		$last_login_ip = $row['last_login_ip'];

	return $last_login_ip;
	}

//Function to fetch Users Lat login date
	function get_last_login($user_id)
	{
		$result = mysql_query("SELECT DATE_FORMAT(last_login, '%b %e,%y  %l:%i:%p') as user_lastlogindate  FROM users WHERE user_id = '$user_id'"); 
		$row = mysql_fetch_assoc($result);
		$last_login = $row['user_lastlogindate'];
		
	return $last_login;
	}

//Functions to fetch user's Full name
	function get_user_fullname($user_id)
	{
		$result = mysql_query("SELECT * FROM users WHERE user_id = '$user_id'"); 
		$row = mysql_fetch_assoc($result);
		$first_name = $row['first_name'];
		$other_names = $row['other_names'];
		
		$user_fullname = $first_name . " " . $other_names;

	return $user_fullname;
	}
	
	
//Function to fetch user name
	function get_user_username($user_id)
	{
		$result = mysql_query("SELECT * FROM users WHERE user_id = '$user_id'"); 
		$row = mysql_fetch_assoc($result);
		$username = $row['username'];
		

	return $username;
	}
	
	
	
//Function to fetch email
	function get_user_email($user_id)
	{
		$result = mysql_query("SELECT * FROM users WHERE user_id = '$user_id'"); 
		$row = mysql_fetch_assoc($result);
		$email = $row['email'];

	return $email;
	}
	
	
	//function dat get user security id  by id 
	function get_security_id($user_id)
	{
		$q = "SELECT security_id FROM users WHERE user_id = '$user_id' ";
		$r = mysql_query($q);
		$row = mysql_fetch_assoc($r);
		
		return $row['security_id'];
		
		
	}
	
	
	//function dat get user image by id 
	function get_user_image($user_id)
	{
	$q = "SELECT image FROM users WHERE user_id = '$user_id' ";
	$r = mysql_query($q);
	$row = mysql_fetch_assoc($r);
	
	return $row['image'];
	
	
	}
	
	
	//function dat get user image by application number 
	function get_user_image_application_no($apn)
	{
		$q = "SELECT image FROM applicants_2019 WHERE application_number = '$apn'";
		$r = mysql_query($q);
		$row = mysql_fetch_assoc($r);
		
		return $row['image'];
	
	
	}
	
	
	
	//Function to fetch role name
	function get_role_title($role_id)
	{
		$result = mysql_query("SELECT * FROM roles WHERE id = '$role_id'"); 
		$row = mysql_fetch_assoc($result);
		$role_title = $row['role_title'];
		
	return $role_title;
	}


	
	//=========================================== get posted by by passing security ID == ===========/
	function get_posted_by($security_id)
	{	
		$query = "SELECT first_name
				 
				 FROM users
				 
				 WHERE security_id = '$security_id'";
					
		$result = mysql_query($query); 
							
		$row = mysql_fetch_array($result,MYSQL_ASSOC);
		$poster = $row['first_name'];
		
	return $poster;
	}
	
	
	//============================================ latest updates index.php =========================================//
	//function that gets tge latest four news on index.php
	function get_latest_updates()
	{
		$q = "SELECT id,title,LEFT(description, 250) as description, image, DATE_FORMAT(date_added, '%b %e, %Y %l:%i:%p') as date_added, added_by

		FROM news
		
		ORDER BY date_added DESC
		
		LIMIT 0,4";
		$r = mysql_query($q);
		
		if(mysql_num_rows($r)>0)
		{
			while($row = mysql_fetch_array($r,MYSQL_ASSOC))
			{
				
				///extract the description so dat 100 words can be displayed only
				$extract_description = $row['description'];
				$get_last_space = strrpos($extract_description, ' ');
				$extracted_description = substr($extract_description, 0, $get_last_space);
				
				
				$output  = '<div class="col-md-3 text-center">';
				$output .= '<img class="img-rounded img-thumbnail img-responsive center-block" width="200px" height="200px" 
							src="administrator/uploads/pictures/news_images/'.$row['image'].'" />';
				$output .= '<br>';
				$output .= '<h4><b>'.$row['title'].'</b></h4><span class="label label-default">'.$row['date_added'].'</span>';
				$output .= '<p>'.$extracted_description.' - ';
				$output .=  get_posted_by($row['added_by']).'</p>';
				$output .= '<a class="btn btn-sm btn-primary" href="single_news.php?i='.sha1($row['id']).'"> Read More</a>';
				$output .= '</div>';
				
				
				echo $output;
			}
		}
		
		
	}//end of get latest update
	
	
	//function get lates events on index.php 
	function get_latest_events()
	{
		$q = "SELECT id,title,LEFT(description, 100) as description, image, DATE_FORMAT(date_added, '%b %e, %Y %l:%i:%p') as date_added, added_by

		FROM events
		
		ORDER BY date_added DESC
		
		LIMIT 0,5";
		$r = mysql_query($q);
		
		if(mysql_num_rows($r)>0)
		{
			while($row = mysql_fetch_array($r,MYSQL_ASSOC))
			{
				
				
				$output  = '<a class="list-group-item" href="single_event.php?i='.sha1($row['id']).'">';
				$output  .= $row['title'];
				$output .= '<span class="glyphicon glyphicon-chevron-right"></span>';
				$output  .= '</a>';
				
				echo $output;
			}
		}
		
	}//end of get latest updates
	
	/*======================================  index functions end ==============================*/



/* ============================================== news functions begn ===============================*/

	
//function view news

function show_news()
{
	
	
	//collect the page number, sorting details and dis[lay per page
	//no of display per page
	$display_per_page = 10;
	
	
	//collect the page variable from url
	 //determin how many pages there are
	if (isset($_GET['page']) && is_numeric($_GET['page']))
	{//already determinwd
		$pages = $_GET['page'];
	
	}
	else
	{//needs to be determine
	  
	  
		//get total projects base user role, mda
		$q = "SELECT COUNT(id) 
			
			  FROM  news";
		$r = @mysql_query($q);
			
		$row = @mysql_fetch_array($r, MYSQL_NUM);
		//records holds the result of the sql
		$records = $row[0];
			
			
		
		///calculate the no of pages
		if($records > $pages)
		{
			$pages = ceil($records/$display_per_page);
		}
		else
		{
			$pages = 1;
		}
		
		
		
		
	}//end of pages if
	
	
								  
								 
	
	//get the start of the query 
	//determin in the db where to start reurning result
	if(isset($_GET['start']) && is_numeric($_GET['start']))
	{
		$start = $_GET['start'];
	}
	else
	{
		$start = 0;
	}
	
	
	//determine the sorting
	$sort = (isset($_GET['sort'])?$_GET['sort']: 'date_added DESC');
	
	//swith the order
	switch($sort)
	{
		case 'title':
		$order_by = 'title ASC';
		
		break;
		
		case 'description':
		$order_by = 'description ASC';
		
		break;
		
		
		case 'added_by':
		$order_by = 'added_by ASC';
		
		break;
		
		
		case 'date_added':
		$order_by = 'date_added ASC';
		
		break;
		
		case 'date_modified':
		$order_by = 'date_added ASC';
		
		break;
		
		
		default:
		$order_by = 'date_added DESC';
		
		break;
		
		
	}
	
	
	
	
	//query the database
	$q = "SELECT id, title, LEFT(description, 150) as description, image,		  
		 DATE_FORMAT(date_added, '%M %d, %Y %l:%i:%p') as date_added,
		 DATE_FORMAT(date_modified, '%M %d, %Y %l:%i:%p') as date_modified, added_by
		 FROM  news
		 ORDER BY $order_by LIMIT $start , $display_per_page";
	$r = mysql_query($q);
	
	if(mysql_num_rows($r) > 0)
	{//show table
	
		while($row = mysql_fetch_array($r, MYSQL_ASSOC))
		{
			///extract the description so dat 100 words can be displayed only
			$extract_description = $row['description'];
			$get_last_space = strrpos($extract_description, ' ');
			$extracted_description = substr($extract_description, 0, $get_last_space);
			
			
			$output = '<div class="col-md-12 text-center">';
			$output .= '<div class="col-md-6 text-center">';
			$output .='<img class="img-rounded  img-responsive center-block" width="200px" height="200px" 
						src="administrator/uploads/pictures/news_images/'.$row['image'].'" />' ;
			$output .= '<br>';
			
			$output .= '</div>'; 
			$output .= '<div class="col-md-6 text-left">';
			$output .= '<h4><b>'.$row['title'].'</b></h4> <span class="label label-default">'.$row['date_added'].'</span>';
			$output .= '<p>'.$extracted_description.' - '.get_posted_by($row['added_by']).'</p>';
			$output .= '<a class="btn btn-sm btn-primary" href="single_news.php?i='.sha1($row['id']).'"> Read More</a>';
			$output .= '</div><br/><hr/>';
			$output .= '</div>';
			$output .= '<hr><br>';
			
			echo $output;
			
		}
	}
	
	
	//check if pages is greater than 1
	if($pages > 1)
	{
		echo '<div class="text-center"><ul class="pagination">';
		
		//determine the current page
		$current_page = ($start/$display_per_page)+1;
		
		//if its not a current page display a previous link
		if($current_page != 1)
		{
			echo '<li><a href="news.php?sort='.$sort.'&start='.($start - $display_per_page).'&page='.$pages.'" >«</a></li>';
		}
		
		
		//make all the numbered pages
		for($i = 1; $i <= $pages; $i++)
		{
			if($i != $current_page)
			{
				echo 
				'<li><a href="news.php?sort='.$sort.'&start='.(($display_per_page * ($i - 1)))  .'&page='.$pages.'" >' . $i . '</a> </li>';
			}
			else
			{
				echo  '<li><a href="#">'. $i . '</a></li> ';
			}
		}//end of forloop
		
		
		//if itsnt d last page get the necxt link
		if($current_page != $pages)
		{
			echo '<li><a href="news.php?sort='.$sort.'&start='.($start + $display_per_page).'&page='.$pages.'">»</a></li>';
			
		}
	}
	
	
	
	
	
	echo '</ul></div>';
								

	
	
	
	
	
}//end of view news


//function that shows the news, title, news date , news posted by 
function get_news_title($id)
{
	$q = "SELECT title FROM news WHERE '$id' = SHA1(id)";
	$r= mysql_query($q);
	
	if(mysql_num_rows($r) >0)
	{
		$row = mysql_fetch_array($r, MYSQL_ASSOC);
		
		$title = $row['title'];
	}
	
	return $title;
	
}

//function that gets the date news was posted 
function get_news_date($id)
{
	$q = "SELECT DATE_FORMAT(date_added, '%M %d, %Y %l:%i:%p') as date_added FROM news WHERE '$id' = SHA1(id)";
	$r= mysql_query($q);
	
	if(mysql_num_rows($r) >0)
	{
		$row = mysql_fetch_array($r, MYSQL_ASSOC);
		
		$date_added = $row['date_added'];
	}
	
	return $date_added;
	
}
//end of get when news was posted


//function that  news posted by on signle_news.php page
function get_news_poster($id)
{
	$q = "SELECT n.id as nid, n.title, n.added_by, u.user_id as uid, u.first_name, u.security_id 
	   FROM news n , users u
	  
	   WHERE 
	   u.security_id = n.added_by
		AND '$id' = SHA1(id)";
	$r= mysql_query($q);
	
	if(mysql_num_rows($r) >0)
	{
		$row = mysql_fetch_array($r, MYSQL_ASSOC);
		
		$first_name = $row['first_name'];
	}
	
	return $first_name;
	
}//end of get poster for a single page


//function that gets the image for single_news.php
function get_news_image($id)
{
	$q = "SELECT image FROM news WHERE '$id' = SHA1(id)";
	$r= mysql_query($q);
	
	if(mysql_num_rows($r) >0)
	{
		$row = mysql_fetch_array($r, MYSQL_ASSOC);
		
		$image = $row['image'];
	}
	
	return $image;
	
}
//end of get when image




//function that gets news description for single_news.php
function get_news_description($id)
{
	$q = "SELECT description FROM news WHERE '$id' = SHA1(id)";
	$r= mysql_query($q);
	
	if(mysql_num_rows($r) >0)
	{
		$row = mysql_fetch_array($r, MYSQL_ASSOC);
		
		$description = $row['description'];
	}
	
	return $description;
	
}
//end of get when description




//show eevnts on single_news.php page
function show_5_events()
{
	
	
	//query the database
	$q = "SELECT id, title, LEFT(description, 150) as description, image,		  
		 DATE_FORMAT(date_added, '%M %d, %Y %l:%i:%p') as date_added,
		 DATE_FORMAT(date_modified, '%M %d, %Y %l:%i:%p') as date_modified, added_by
		 FROM  events
		 ORDER BY date_added DESC LIMIT 0 , 5";
	$r = mysql_query($q);
	
	if(mysql_num_rows($r) > 0)
	{//show table
	
		while($row = mysql_fetch_array($r, MYSQL_ASSOC))
		{
			///extract the description so dat 100 words can be displayed only
			$extract_description = $row['description'];
			$get_last_space = strrpos($extract_description, ' ');
			$extracted_description = substr($extract_description, 0, $get_last_space);
			
			
			$output = '<div class="col-md-12 text-center">';
			$output .= '<div class="col-md-6 text-center">';
			$output .='<img class="img-circle center-block" width="100px" height="100px" 
						src="administrator/uploads/pictures/events_images/'.$row['image'].'" />' ;
			$output .= '<br>';
			
			$output .= '</div>'; 
			$output .= '<div class="col-md-6">';
			$output .= '<h4><b>'.$row['title'].'</b></h4>';
			$output .= '<p>'.$extracted_description.'</p>';
			$output .= '<a class="btn btn-sm btn-primary" href="single_event.php?i='.sha1($row['id']).'"> Read More</a>';
			$output .= '</div><br/><hr/>';
			$output .= '</div>';
			$output .= '';
			
			echo $output;
			
		}
	}
	
	
								

	
	
	
	
	
}//end of events single_news.php


/* ======================================= end of news funtion =====================================*/


function show_events()
{
	
	
	//collect the page number, sorting details and dis[lay per page
	//no of display per page
	$display_per_page = 10;
	
	
	//collect the page variable from url
	 //determin how many pages there are
	if (isset($_GET['page']) && is_numeric($_GET['page']))
	{//already determinwd
		$pages = $_GET['page'];
	
	}
	else
	{//needs to be determine
	  
	  
		//get total projects base user role, mda
		$q = "SELECT COUNT(id) 
			
			  FROM  events";
		$r = @mysql_query($q);
			
		$row = @mysql_fetch_array($r, MYSQL_NUM);
		//records holds the result of the sql
		$records = $row[0];
			
			
		
		///calculate the no of pages
		if($records > $pages)
		{
			$pages = ceil($records/$display_per_page);
		}
		else
		{
			$pages = 1;
		}
		
		
		
		
	}//end of pages if
	
	
								  
								 
	
	//get the start of the query 
	//determin in the db where to start reurning result
	if(isset($_GET['start']) && is_numeric($_GET['start']))
	{
		$start = $_GET['start'];
	}
	else
	{
		$start = 0;
	}
	
	
	//determine the sorting
	$sort = (isset($_GET['sort'])?$_GET['sort']: 'date_added DESC');
	
	//swith the order
	switch($sort)
	{
		case 'title':
		$order_by = 'title ASC';
		
		break;
		
		case 'description':
		$order_by = 'description ASC';
		
		break;
		
		
		case 'added_by':
		$order_by = 'added_by ASC';
		
		break;
		
		
		case 'date_added':
		$order_by = 'date_added ASC';
		
		break;
		
		case 'date_modified':
		$order_by = 'date_added ASC';
		
		break;
		
		
		default:
		$order_by = 'date_added DESC';
		
		break;
		
		
	}
	
	
	
	
	//query the database

	$q = "SELECT id, title, LEFT(description, 150) as description, image,		  
		 DATE_FORMAT(date_added, '%M %d, %Y %l:%i:%p') as date_added,
		 DATE_FORMAT(date_modified, '%M %d, %Y %l:%i:%p') as date_modified, added_by
		 FROM  events
		 ORDER BY $order_by LIMIT $start , $display_per_page";
	$r = mysql_query($q);
	
	if(mysql_num_rows($r) > 0)
	{//show table
	
		while($row = mysql_fetch_array($r, MYSQL_ASSOC))
		{
			///extract the description so dat 100 words can be displayed only
			$extract_description = $row['description'];
			$get_last_space = strrpos($extract_description, ' ');
			$extracted_description = substr($extract_description, 0, $get_last_space);
			
			
			$output = '<div class="col-md-12 text-center">';
			$output .= '<div class="col-md-6 text-center">';
			$output .='<img class="img-thumbnail  img-responsive center-block" width="200px" height="200px" 
						src="administrator/uploads/pictures/events_images/'.$row['image'].'" />' ;
			$output .= '<br>';
			
			$output .= '</div>'; 
			$output .= '<div class="col-md-6">';
			$output .= '<h4><b>'.$row['title'].'</b></h4> <span class="label label-default">'.$row['date_added'].'</span>';
			$output .= '<p>'.$extracted_description.'</p>';
			$output .= '<a class="btn btn-sm btn-primary" href="single_event.php?i='.sha1($row['id']).'"> Read More</a>';
			$output .= '</div><br/><hr/>';
			$output .= '</div>';
			$output .= '';
			
			echo $output;
			
		}
	}
	
	
	//check if pages is greater than 1
	if($pages > 1)
	{
		echo '<div class="text-center"><ul class="pagination">';
		
		//determine the current page
		$current_page = ($start/$display_per_page)+1;
		
		//if its not a current page display a previous link
		if($current_page != 1)
		{
			echo '<li><a href="events.php?sort='.$sort.'&start='.($start - $display_per_page).'&page='.$pages.'" >«</a></li>';
		}
		
		
		//make all the numbered pages
		for($i = 1; $i <= $pages; $i++)
		{
			if($i != $current_page)
			{
				echo 
				'<li><a href="events.php?sort='.$sort.'&start='.(($display_per_page * ($i - 1)))  .'&page='.$pages.'" >' . $i . '</a> </li>';
			}
			else
			{
				echo  '<li><a href="#">'. $i . '</a></li> ';
			}
		}//end of forloop
		
		
		//if itsnt d last page get the necxt link
		if($current_page != $pages)
		{
			echo '<li><a href="events.php?sort='.$sort.'&start='.($start + $display_per_page).'&page='.$pages.'">»</a></li>';
			
		}
	}
	
	
	
	
	
	echo '</ul></div>';
								

	
	
	
	
	
}//end of view events



//function that shows the events, title, events date , events posted by 
function get_event_title($id)
{
	$q = "SELECT title FROM events WHERE '$id' = SHA1(id)";
	$r= mysql_query($q);
	
	if(mysql_num_rows($r) >0)
	{
		$row = mysql_fetch_array($r, MYSQL_ASSOC);
		
		$title = $row['title'];
	}
	
	return $title;
	
}

//function that gets the date event was posted 
function get_event_date($id)
{
	$q = "SELECT DATE_FORMAT(date_added, '%M %d, %Y %l:%i:%p') as date_added FROM events WHERE '$id' = SHA1(id)";
	$r= mysql_query($q);
	
	if(mysql_num_rows($r) >0)
	{
		$row = mysql_fetch_array($r, MYSQL_ASSOC);
		
		$date_added = $row['date_added'];
	}
	
	return $date_added;
	
}
//end of get when events was posted


//function that  event posted by on signle_event.php page
function get_event_poster($id)
{
	$q = "SELECT e.id as eid, e.title, e.added_by, u.user_id as uid, u.first_name, u.security_id 
	   FROM events e , users u
	  
	   WHERE 
	   u.security_id = e.added_by
		AND '$id' = SHA1(id)";
	$r= mysql_query($q);
	
	if(mysql_num_rows($r) >0)
	{
		$row = mysql_fetch_array($r, MYSQL_ASSOC);
		
		$first_name = $row['first_name'];
	}
	
	return $first_name;
	
}//end of get poster for a single page


//function that gets the image for single_event.php
function get_event_image($id)
{
	$q = "SELECT image FROM events WHERE '$id' = SHA1(id)";
	$r= mysql_query($q);
	
	if(mysql_num_rows($r) >0)
	{
		$row = mysql_fetch_array($r, MYSQL_ASSOC);
		
		$image = $row['image'];
	}
	
	return $image;
	
}
//end of get when image




//function that gets events description for single_event.php
function get_event_description($id)
{
	$q = "SELECT description FROM events WHERE '$id' = SHA1(id)";
	$r= mysql_query($q);
	
	if(mysql_num_rows($r) >0)
	{
		$row = mysql_fetch_array($r, MYSQL_ASSOC);
		
		$description = $row['description'];
	}
	
	return $description;
	
}
//end of get when description



//show eevnts on single_event.php page
function show_5_news()
{
	
	
	//query the database
	$q = "SELECT id, title, LEFT(description, 100) as description, image,		  
		 DATE_FORMAT(date_added, '%M %d, %Y %l:%i:%p') as date_added,
		 DATE_FORMAT(date_modified, '%M %d, %Y %l:%i:%p') as date_modified, added_by
		 FROM  news
		 ORDER BY date_added DESC LIMIT 0 , 5";
	$r = mysql_query($q);
	
	if(mysql_num_rows($r) > 0)
	{//show table
	
		while($row = mysql_fetch_array($r, MYSQL_ASSOC))
		{
			///extract the description so dat 100 words can be displayed only
			$extract_description = $row['description'];
			$get_last_space = strrpos($extract_description, ' ');
			$extracted_description = substr($extract_description, 0, $get_last_space);
			
			
			$output = '<div class="col-md-12 text-center">';
			$output .= '<div class="col-md-6 text-center">';
			$output .='<img class="img-circle center-block" width="100px" height="100px" 
						src="administrator/uploads/pictures/news_images/'.$row['image'].'" />' ;
			$output .= '<br>';
			
			$output .= '</div>'; 
			$output .= '<div class="col-md-6">';
			$output .= '<h4><b>'.$row['title'].'</b></h4>';
			$output .= '<p>'.htmlentities ($extracted_description).'</p>';
			$output .= '<a class="btn btn-sm btn-primary" href="single_news.php?i='.sha1($row['id']).'"> Read More</a>';
			$output .= '</div><br/><hr/>';
			$output .= '</div>';
			$output .= '';
			
			echo $output;
			
		}
	}
	
	
	
	
}//end of events single_event.php


/* ============================================================= end of event functions =================================*/



//function select state 
function select_state( $state)
{
	$r = mysql_query("SELECT id, title  FROM state  ORDER BY title ");
	if(mysql_num_rows($r) > 0 )
	{
		while($row = mysql_fetch_array($r, MYSQL_ASSOC))
		{
			if($state == $row['id'])
			{
				$output = "<option value=\"$row[id]\" selected>";
				$output .=  $row['title'];
				$output .="</option>";
				
			}
			else
			{
				$output = "<option value=\"$row[id]\" >";
				$output .= $row['title'];
				$output .="</option>";
				
			}
			 
			
			echo $output;
		}
	}
	else
	{
		
			$output = "<option>";
			$output .="Not added yet";
			$output .="</option>"; 
			
			echo $output;
	}
}
//end of select state

//function select marital status 
function select_marital_status($st)
{
		
		if ($st == 'single')
		{
			$output = '<option value="0" > -- Please Select --</option>';
			$output .= '<option value="single" selected > Single</option>';
			$output .= '<option value="married" > Married</option>';
			$output .= '<option value="divorced" >Divorced</option>';
			$output .= '<option value="widowed" >Widowed</option>';
		
			
		}
		else if ($st == 'married')
		{
			$output = '<option value="0" > -- Please Select --</option>';
			$output .= '<option value="single"  > Single</option>';
			$output .= '<option value="married" selected> Married</option>';
			$output .= '<option value="divorced" >Divorced</option>';
			$output .= '<option value="widowed" >Widowed</option>';
		
			
		}
		else if ($st == 'divorced')
		{
			$output = '<option value="0" > -- Please Select --</option>';
			$output .= '<option value="single"  > Single</option>';
			$output .= '<option value="married" > Married</option>';
			$output .= '<option value="divorced" selected >Divorced</option>';
			$output .= '<option value="widowed" >Widowed</option>';
		
			
		}
		else if ($st == 'widowed')
		{
			$output = '<option value="0" > -- Please Select --</option>';
			$output .= '<option value="single"  > Single</option>';
			$output .= '<option value="married" > Married</option>';
			$output .= '<option value="divorced"  >Divorced</option>';
			$output .= '<option value="widowed" selected >Widowed</option>';
		
			
		}
		else
		{
		
			$output = '<option value="0" selected> -- Please Select --</option>';
			$output .= '<option value="single"  > Single</option>';
			$output .= '<option value="married" > Married</option>';
			$output .= '<option value="divorced" >Divorced</option>';
			$output .= '<option value="widowed" >Widowed</option>';
		
		}

	return $output;

}
//end of select marrital status


function select_lga( $lga)
{
	$r = mysql_query("SELECT id, title  FROM lga  ORDER BY title ");
	if(mysql_num_rows($r) > 0 )
	{
		while($row = mysql_fetch_array($r, MYSQL_ASSOC))
		{
			if($lga == $row['id'])
			{
				$output = "<option value=\"$row[id]\" selected>";
				$output .=  $row['title'];
				$output .="</option>";
				
			}
			else
			{
				$output = "<option value=\"$row[id]\" >";
				$output .= $row['title'];
				$output .="</option>";
				
			}
			 
			
			echo $output;
		}
	}
	else
	{
		
			$output = "<option>";
			$output .="Not added yet";
			$output .="</option>"; 
			
			echo $output;
	}
}
//end of select lga


//select department
function select_department($department)
{
	$r = mysql_query("SELECT id, title  FROM departments  WHERE real_ = 0 ORDER BY title");
	if(mysql_num_rows($r) > 0 )
	{
		while($row = mysql_fetch_array($r, MYSQL_ASSOC))
		{
			if($department == $row['id'])
			{
				$output = "<option value=\"$row[id]\" selected>";
				$output .=  $row['title'];
				$output .="</option>";
				
			}
			else
			{
				$output = "<option value=\"$row[id]\" >";
				$output .= $row['title'];
				$output .="</option>";
				
			}
			 
			
			echo $output;
		}
	}
	else
	{
		
			$output = "<option>";
			$output .="Not added yet";
			$output .="</option>"; 
			
			echo $output;
	}
}
//end of select department




//select course
function select_course( $course)
{
	$r = mysql_query("SELECT id, title  FROM courses  ORDER BY title");
	if(mysql_num_rows($r) > 0 )
	{
		while($row = mysql_fetch_array($r, MYSQL_ASSOC))
		{
			if($course == $row['id'])
			{
				$output = "<option value=\"$row[id]\" selected>";
				$output .=  $row['title'];
				$output .="</option>";
				
			}
			else
			{
				$output = "<option value=\"$row[id]\" >";
				$output .= $row['title'];
				$output .="</option>";
				
			}
			 
			
			echo $output;
		}
	}
	else
	{
		
			$output = "<option>";
			$output .="Not added yet";
			$output .="</option>"; 
			
			echo $output;
	}
}
//end of select course



/* ################################# function that generates Transaxfrtion Id for applicants %%%%%%%%%%%%%%%%%%%%%%%%%% */
function generate_transaction_id()
{
	$result = mysql_query("SELECT * FROM generate_transaction_id");
	while($row = mysql_fetch_array($result))
	{
		$g_code = $row['code'];
	}
	
	//add 1 to $g_code and tek to d db
	$g_coded = $g_code + 1;
	
	mysql_query("UPDATE generate_transaction_id  SET code = '$g_coded'");
	
	
	//add random four values before adding to the database
	$add_random_value = mt_rand(100, 999);
	$code = $add_random_value.$g_coded;
	$tid = date("Ymd-$code");
	
	return $tid;
}

/* end of function that generates transaction id */


//function calendar yer
function year($y)
{
	$y = $y;
	$years = range(1980, 2017);
	
	foreach ($years as $value) 
	{
		if($y == $value)
		{
	   		echo "<option value=\"$value\" selected>$value</option>\n";
		}
		else
		{
			echo "<option value=\"$value\">$value</option>\n";
		}
	}
	
	
}//end of the year




//function calendar months
function month($m)
{
	$m = $m;
	$months = range(1, 12);
	
	foreach ($months as $value) 
	{
		if($m == $value)
		{
	   		echo "<option value=\"$value\" selected>$value</option>\n";
		}
		else
		{
			echo "<option value=\"$value\">$value</option>\n";
		}
	}
	
	
}




//function calendardays
function day($d)
{
	$d = $d;
	$days = range(1, 31);
	
	foreach ($days as $value) 
	{
		if($d == $value)
		{
	   		echo "<option value=\"$value\" selected>$value</option>\n";
		}
		else
		{
			echo "<option value=\"$value\">$value</option>\n";
		}
	}
	
	
}
//end of the dau function


/* ################################# function that generates application id %%%%%%%%%%%%%%%%%%%%%%%%%% */
function generate_application_number()
{
	$result = mysql_query("SELECT * FROM generate_application_number");
	while($row = mysql_fetch_array($result))
	{
		$g_code = $row['code'];
	}
	
	//add 1 to $g_code and tek to d db
	$g_coded = $g_code + 1;
	
	mysql_query("UPDATE generate_application_number  SET code = '$g_coded'");
	
	$len_g_coded  = strlen($g_coded);
	
	if($len_g_coded == '1')
	{
		$code = '000'.$g_coded;
	}
	else if($len_g_coded == '2')
	{
		$code = '00'.$g_coded;
	}
	else if($len_g_coded == '3')
	{
		$code = '0'.$g_coded;
	}
	else
	{
		$code = $g_coded;
	}
	
	
	return $code;
}

//function get pin
function get_pin()
{
	$v = $_SESSION['pin'];

	return $v;
}




/* ################################# function that get application id of user %%%%%%%%%%%%%%%%%%%%%%%%%% */
function get_application_number($pn)
{
	$result = mysql_query("SELECT * FROM users WHERE pin = '$pn'"); 
		$row = mysql_fetch_assoc($result);
		$application_number = $row['application_number'];

	return $application_number;
}



//function that view pdf files
function view_pdf_file($apn)
{
	$result = mysql_query("SELECT * FROM users WHERE application_number = '$apn'"); 
		$row = mysql_fetch_assoc($result);
		$pdf_file = $row['pdf_file'];

	
	if($pdf_file == '')
	{
		return '';
	}
	else
	{
		return '<a class="btn btn-md btn-primary" target="_blank" 
			   href="application_files/'.$pdf_file.'"> View '.$pdf_file.'</a>';
	}
	
}




/* ################################# function that get file uploaded result1 %%%%%%%%%%%%%%%%%%%%%%%%%%  $apn - application number*/
/*function view_result_1($apn)
{
	$result = mysql_query("SELECT * FROM users WHERE application_number = '$apn'"); 
		$row = mysql_fetch_assoc($result);
		$result_1 = $row['result_1'];
		
		
		if($result_1 == '')
		{
			return '';
		}
		else
		{
			return '<a class="btn btn-md btn-primary" target="_blank" 
                   href="application_files/'.$apn.'/'.$result_1.'"> View '.$result_1.'</a>';
		}
		
	
}


//rsult 2
function view_result_2($apn)
{
	$result = mysql_query("SELECT * FROM users WHERE application_number = '$apn'"); 
		$row = mysql_fetch_assoc($result);
		$result_2 = $row['result_2'];

		
		if($result_2 == '')
		{
			return '';
		}
		else
		{
			return '<a class="btn btn-md btn-primary" target="_blank" 
                   href="application_files/'.$apn.'/'.$result_2.'"> View '.$result_2.'</a>';
		}
		
		

}


//primary cert
function view_primary_cert($apn)
{
	$result = mysql_query("SELECT * FROM users WHERE application_number = '$apn'"); 
		$row = mysql_fetch_assoc($result);
		$primary_cert = $row['primary_cert'];

	if($primary_cert == '')
	{
		return '';
	}
	else
	{
		return '<a class="btn btn-md btn-primary" target="_blank" 
			   href="application_files/'.$apn.'/'.$primary_cert.'"> View '.$primary_cert.'</a>';
	}

}


//secondary cert
function view_secondary_cert($apn)
{
	$result = mysql_query("SELECT * FROM users WHERE application_number = '$apn'"); 
		$row = mysql_fetch_assoc($result);
		$secondary_cert = $row['secondary_cert'];

	
	if($secondary_cert == '')
	{
		return '';
	}
	else
	{
		return '<a class="btn btn-md btn-primary" target="_blank" 
			   href="application_files/'.$apn.'/'.$secondary_cert.'"> View '.$secondary_cert.'</a>';
	}
	
}

//indigene cert
function view_indigene_cert($apn)
{
	$result = mysql_query("SELECT * FROM users WHERE application_number = '$apn'"); 
		$row = mysql_fetch_assoc($result);
		$indigene_cert = $row['indigene_cert'];

	if($indigene_cert == '')
	{
		return '';
	}
	else
	{
		return '<a class="btn btn-md btn-primary" target="_blank" 
			   href="application_files/'.$apn.'/'.$indigene_cert.'"> View '.$indigene_cert.'</a>';
	}
	//end of if 
}
*/


//get course name
function get_course_title($id)
{
	$result = mysql_query("SELECT * FROM courses WHERE id = '$id'"); 
		$row = mysql_fetch_assoc($result);
		$title = $row['title'];

	return $title;
}
//


//get departmet id base on user id
//function that gets  department base on user id
function  get_department_id($user_id)
{	
	$result = mysql_query("SELECT * FROM users WHERE user_id = '$user_id'"); 
		$row = mysql_fetch_assoc($result);
		$department_id = $row['department_id'];

	return $department_id;
	
}


//function that gets course  base on user id
function  get_course_id($user_id)
{	
	$result = mysql_query("SELECT * FROM users WHERE user_id = '$user_id'"); 
		$row = mysql_fetch_assoc($result);
		$course_id = $row['course_id'];

	return $course_id;
	
}

//function that gets department id by user id
function  getDept($user_id)
{	
	$result = mysql_query("SELECT * FROM users WHERE user_id = '$user_id'"); 
		$row = mysql_fetch_assoc($result);
		$course_id = $row['department_id'];

	return $course_id;
	
}

function check_file_uploads($apn)
{
	    $result = mysql_query("SELECT pdf_file FROM users WHERE application_number = '$apn'"); 
		$row = mysql_fetch_assoc($result);
		$pdf_file = $row['pdf_file'];
		
		
		
		//if all od them are empty
		if($pdf_file == '')
		{
			return '<span class="btn btn-md btn-primary" > Please upload all the required files </span>';
		}//if both result are empty
		else
		{
			//do nothing
			return '<a class="btn btn-md btn-primary" href="page_preview.php"> Preview Submitted Details</a>';
			
		}
		
}

//end of check if upload files are intact





/*//check file uploads base on application name
function check_file_uploads($apn)
{
	$result = mysql_query("SELECT * FROM users WHERE application_number = '$apn'"); 
		$row = mysql_fetch_assoc($result);
		$result_1 = $row['result_1'];
		$result_2 = $row['result_2'];
		$primary_cert = $row['primary_cert'];
		$secondary_cert = $row['secondary_cert'];
		$indigene_cert = $row['indigene_cert'];
		
		
		//if all od them are empty
		if(($result_1 == '' || $result_2 == '') && ($primary_cert == '' && $secondary_cert == '' && $indigene_cert == '' ))
		{
			return '<span class="btn btn-md btn-primary" > Please upload all the required files </span>';
		}//if both result are empty
		else if(($result_1 == '' || $result_2 == '') && ($primary_cert != '' && $secondary_cert != '' && $indigene_cert != '' ))
		{
			return '<span class="btn btn-md btn-primary" > Please upload all the required files </span>';
		}//if only result 1 is empty
		else if(($result_1 == '' || $result_2 != '') && ($primary_cert != '' && $secondary_cert != '' && $indigene_cert != '' ))
		{
			return '<a class="btn btn-md btn-primary" href="page_preview.php"> Preview Submitted Details</a>';
		}//if only result 2 is empty
		else if(($result_1 != '' || $result_2 == '') && ($primary_cert != '' && $secondary_cert != '' && $indigene_cert != '' ))
		{
			return '<a class="btn btn-md btn-primary" href="page_preview.php"> Preview Submitted Details</a>';
		}//if all result 1 and all result 2 are nt  empty and primary is empty
		else if (($result_1 != '' || $result_2 != '') && ($primary_cert == '' && $secondary_cert != '' && $indigene_cert != ''))
		{
			return '<span class="btn btn-md btn-primary" > Please upload all the required files </span>';
			
		}//if result 1 and result 2 not empty , primary cert nt empty, secondary cert  empty and indigene cert nt empty
		else if (($result_1 != '' || $result_2 != '') && ($primary_cert != '' && $secondary_cert == '' && $indigene_cert != ''))
		{
			return '<span class="btn btn-md btn-primary" > Please upload all the required files </span>';
			
		}//if only indigene cert is empty
		else if (($result_1 != '' || $result_2 != '') && ($primary_cert != '' && $secondary_cert != '' && $indigene_cert == ''))
		{
			return '<span class="btn btn-md btn-primary" > Please upload all the required files </span>';
			
		}//if all of dem are nt empty
		else if (($result_1 != '' || $result_2 != '') && ($primary_cert != '' && $secondary_cert != '' && $indigene_cert != ''))
		{
			return '<a class="btn btn-md btn-primary" href="page_preview.php"> Preview Submitted Details</a>';
		}
		else
		{
			// do nothing for nw
		}
}

//end of check if upload files are intact
*/

//function that checks if user already has application number using pin
function has_application_number($pin)
{
	
		$result = mysql_query("SELECT * FROM users WHERE pin = '$pin'"); 
		$row = mysql_fetch_assoc($result);
		$application_number = $row['application_number'];
		
		
		
		if($application_number != '')
		{
			return true;
		}
		else
		{
			return false;
		}

	
	
}//end of if user has application number

	
	//funcion that gets subject name base on subject id
	function get_subject_name($user_id)
	{
		$result = mysql_query("SELECT * FROM subject WHERE subject_id = '$user_id'"); 
		$row = mysql_fetch_assoc($result);
		$subjecttitle = $row['subject_title'];
		

	return $subjecttitle;
	}
	

	//function that get subject code base on subject id
	function get_subject_code($user_id)
	{
		$result = mysql_query("SELECT * FROM subject WHERE subject_id = '$user_id'"); 
		$row = mysql_fetch_assoc($result);
		$subjecttitle = $row['subject_code'];
		

	return $subjecttitle;
	}
	
	//function that gets subject unit base on subject unit
	function get_subject_unit($user_id)
	{
		$result = mysql_query("SELECT * FROM subject WHERE subject_id = '$user_id'"); 
		$row = mysql_fetch_assoc($result);
		$subjecttitle = $row['subject_unit'];
		

	return $subjecttitle;
	}
	
	//function that gets subjects semester belongs to, using subject id
	function get_subject_semester($user_id)
	{
		$result = mysql_query("SELECT * FROM subject WHERE subject_id = '$user_id'"); 
		$row = mysql_fetch_assoc($result);
		$subjecttitle = $row['semester'];
		

	return $subjecttitle;
	}
	
	//function get image name base on pin passed
	
	function get_image_name($pin)
	{
		//collect the former name of the image
		$result = mysql_query("SELECT * FROM users WHERE pin = '$pin'"); 
		$row = mysql_fetch_assoc($result);
		$image = $row['image'];
		

	return $image;
			//end of checking if file exist 
	}
	
	
	
	
	//function to check if application or form sales is on // application is on 1
	function get_application_status()
	{
		$result = mysql_query("SELECT * FROM control WHERE id = 1");
		$row = mysql_fetch_assoc($result);
		$status = $row['status'];
		
		if($status == 1)
		{
			return true;
		}
		else
		{
			return false;
		}
		
		
	}
	
	//function to check if school_resgistration status  1
	function get_school_registration_status()
	{
		$result = mysql_query("SELECT * FROM control WHERE id = 2");
		$row = mysql_fetch_assoc($result);
		$status = $row['status'];
		
		
		if($status == 1)
		{
			return true;
		}
		else
		{
			return false;
		}	
	}
	
	//function to check if school fee payment is on 1
	function get_school_fee_status()
	{
		$result = mysql_query("SELECT * FROM control WHERE id = 3");
		$row = mysql_fetch_assoc($result);
		$status = $row['status'];
		
	
		if($status == 1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	
	//function to check if admission is on 1
	function get_admission_manager_status()
	{
		$result = mysql_query("SELECT * FROM control WHERE id = 4");
		$row = mysql_fetch_assoc($result);
		$status = $row['status'];
		
		
		if($status == 1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	
	//function total applicants
	function get_total_applicants()
	{
		$q = "SELECT COUNT(user_id) FROM users WHERE role_id = 2";
		$r = mysql_query($q);
	
		$row = mysql_fetch_array($r, MYSQL_NUM);
	
		return $row[0];
	}
	
	
	//function that set batch
	function set_batch($pin)
	{
		$total_applicants = get_total_applicants();
		
		
		
		if($total_applicants <= 325)
		{
			$result = mysql_query("UPDATE users SET batch = 1 WHERE pin = '$pin'");
			
		}
		else if ($total_applicants <= 800)
		{
			$result = mysql_query("UPDATE users SET batch = 2 WHERE pin = '$pin'");

		}
                else
                {
                         $result = mysql_query("UPDATE users SET batch = 3 WHERE pin = '$pin'");
                }

		
	}
	//end of set
          
        //function to check if batch exist in the db 
	function batch_exist($pin)
	{
		$result = mysql_query("SELECT batch FROM users WHERE pin = '$pin'");
		$row = mysql_fetch_assoc($result);
		$status = $row['batch'];
		
		if($status == 1 || $status == 2 || $status == 3)
		{
			return true;
		}
		else
		{
			return false;
		}
		
		
	}
	
 
	
	
	//get batch title
	function get_batch_title($id)
	{
		
		$result = mysql_query("SELECT title FROM batches WHERE id = '$id'"); 
		$row = mysql_fetch_assoc($result);
		$title = $row['title'];
		

		return $title;
	}
	
	
	//get batch time
	function get_batch_time($id)
	{
		
		$result = mysql_query("SELECT time FROM batches WHERE id = '$id'"); 
		$row = mysql_fetch_assoc($result);
		$time = $row['time'];
		

		return $time;
	}
	
	
	//get batch date
	function get_batch_date($id)
	{
		
		$result = mysql_query("SELECT date FROM batches WHERE id = '$id'"); 
		$row = mysql_fetch_assoc($result);
		$date = $row['date'];
		

		return $date;
	}
	
//other cert
function view_other_cert($apn)
{
	$result = mysql_query("SELECT * FROM users WHERE application_number = '$apn'"); 
		$row = mysql_fetch_assoc($result);
		$other_cert = $row['other_cert'];

	if($other_cert == '')
	{
		return '';
	}
	else
	{
		return '<a class="btn btn-md btn-primary" target="_blank" 
			   href="application_files/'.$apn.'/'.$other_cert.'"> View '.$other_cert.'</a>';
	}
	//end of if 
	
}


//function that send sms when its on hundreds


//function for report 
//function get total no of pin generated for form sales
function get_total_no_pin_generated($card_type)
{
	$q = "SELECT COUNT(*) FROM pin WHERE card_type = '$card_type'";
	$r = mysql_query($q);
	$row = mysql_fetch_array($r,MYSQL_NUM);
	
	return $row[0];
}


//function get total no of used  pins generated for form sales
function get_total_no_used_pins($card_type)
{
	$q = "SELECT COUNT(*) FROM pin WHERE card_type = '$card_type' AND status = '0'";
	$r = mysql_query($q);
	$row = mysql_fetch_array($r,MYSQL_NUM);
	
	return $row[0];
}




//function get total no of unused  pins generated for form sales
function get_total_no_unused_pins($card_type)
{
	$q = "SELECT COUNT(*) FROM pin WHERE card_type = '$card_type' AND status = '1'";
	$r = mysql_query($q);
	$row = mysql_fetch_array($r,MYSQL_NUM);
	
	return $row[0];
}

//function to check if pin exist in the db else update the record
	function user_pin_exist($pin)
	{
		$result = mysql_query("SELECT pin  FROM users WHERE pin = '$pin'");
		$row = mysql_fetch_assoc($result);
		$status = $row['pin'];
		
		if($status != '')
		{
			return true;
		}
		else
		{
			return false;
		}
		
		
	}


	
	
	//function dat get user id  by passing pin
	function get_user_id_by_pin($pin)
	{
		$q = "SELECT user_id FROM pin_2019_entry WHERE pin = '$pin' ";
		$r = mysql_query($q);
		$row = mysql_fetch_assoc($r);
		
		return $row['user_id'];
		
		
	}
	
	
	//function dat get user id  by passing pin
	function get_user_app_no_by_user_id($id)
	{
		$q = "SELECT application_number FROM applicants_2019 WHERE id = '$id' ";
		$r = mysql_query($q);
		$row = mysql_fetch_assoc($r);
		
		return $row['application_number'];
		
		
	}




?>