Location: PHPKode > projects > The Referral Database > referralsdb/organization_fns.php
<?php
require_once('funcs.mysql.boolean.php');
include('organization.class.php');

function get_categories()
{
   // query database for a list of categories
   $conn = db_connect();
   $query = "select Cat_ID, Cat_Name
             from Categories order by Cat_Name"; 
   $result = mysql_query($query);
   if (!$result)
   {
      echo_db_error();
      return false;
   }
   $num_cats = mysql_num_rows($result);
   if ($num_cats ==0)
   {
      return false;  
   }
   $result = db_result_to_array($result);
   return stripslashes_array($result); 
}

function get_category_name($catid)
 // Expects scalar variable $catid
{
   // query database for the name for a category id
   $conn = db_connect();
   $query = "select Cat_Name
             from Categories 
             where Cat_ID = $catid"; 
   $result = mysql_query($query);
   if (!$result)
     return false;
   $num_cats = mysql_num_rows($result);
   if ($num_cats ==0)
      return false;  
   $result = mysql_result($result, 0, "Cat_Name");
   return stripslashes($result); 
}

function get_organization_categories($orgid) {
 // Expects scalar variable $orgid
   $conn = db_connect();
   $query = "select Organization_Categories.Cat_ID, Organization_Categories.Primary_Category Organizations, Categories.Cat_Name from Organization_Categories, Categories where Organization_Categories.Org_ID = '$orgid' and Organization_Categories.Cat_ID = Categories.Cat_ID order by Cat_Name";
   $result = mysql_query($query);
   if (!$result)
     return false;
   $num_organizations = mysql_num_rows($result);
   if ($num_organizations ==0)
      return false;
   $result = db_result_to_array($result);
   // return 2-dimensional enumerated array to enumerated array
   return stripslashes_array($result);
}

function get_organizations($catid)
 // Expects scalar variable $catid
{
   // query database for the organizations in a category
   if (!$catid || $catid=="")
   {
      print "<B>Error - No category number was recieved.</B><P>";
      return false;
   }

   $conn = db_connect();
   $query = "select Organizations.Org_ID, Organizations.Org_Code, Organizations.Org_Name,  Organizations.Org_Referral_Num, Organizations.Org_Services_Offered from Organizations, Organization_Categories where Organizations.Org_ID = Organization_Categories.Org_ID and Organization_Categories.Cat_ID = '$catid' order by Organizations.Org_Name";
   $result = mysql_query($query);
   if (!$result)
   {
     return false;
   }
   $num_organizations = mysql_num_rows($result);
   if ($num_organizations ==0)
   {
      return false;
   }
   $result = db_result_to_array($result);
   return stripslashes_array($result);
}

function get_organization_details($orgid)
 // Expects scalar variable $orgid
{
  // query database for all details for a particular organization
  if (!$orgid || $orgid=="")
     return false;

   $conn = db_connect();
   $query = "select * from Organizations where Org_ID='$orgid'";
   $result = mysql_query($query);
   if (!$result)
     return false;
   $result = mysql_fetch_array($result);
   // return unidimensional enumerated array
   return stripslashes_array($result);
} 

function keyword_search($keyword) 
// Expects scalar variable $keyword from results.php
{
   $keyword_array = boolean_inclusive_atoms($keyword);
   $errlevel = $keyword_array[0];
   $keyword_string = $keyword_array[1];
   
   if ("2" == $errlevel) {
      // Error 2: Invalid search criteria
      return array(2,$keyword_string);
   }
   
   $table_name="Organizations";
   $conn = db_connect();
   $fulltext_key = get_fulltext_key($table_name,$conn);
   $sql_select_clause = boolean_sql_select($keyword_string, $fulltext_key);
   $sql_where_clause = boolean_sql_where($keyword_string,$fulltext_key);
   $query =  "SELECT Organizations.Org_ID, Organizations.Org_Code, Organizations.Org_Name,  Organizations.Org_Referral_Num, Organizations.Org_Services_Offered, \n"
   .$sql_select_clause." as relevance \n"
   ."FROM $table_name \n"
   ."WHERE \n"
   .$sql_where_clause." \n"
   ."HAVING relevance>0 \n"
   ."ORDER BY relevance DESC \n";
   $result = mysql_query($query);
   #echo $sql_where_clause;
   #echo $query;

   if (!$result)
   {
      return array("0"); 
   }
   $numresults = mysql_num_rows($result);
   if ("0" == $numresults) {
      return array("3"); 
   }
   $searchresults = db_result_to_array($result);
   $searchresults = stripslashes_array($searchresults);

   // return 2-dimensional enumerated array to enumerated array position 0
   // return numresults at position 2
   return array("1",$searchresults,$keyword_string,$numresults);
}


function show_array($array) {
	$elements = count($array);
	for ($x=0;$x<$elements;$x++) {
		echo "$x is $array[$x] <BR>";
	}
}

function stripslashes_uniarray($arr = array()) {
   $rs = array();
   while (list($key,$val) = each($arr)) {
      $rs[$key] = stripslashes($val);
   }
   return $rs;
}


function stripslashes_array($arr = array()) {
   $rs = array();
   while (list($key,$val) = each($arr)) {
      if(is_array($arr[$key])) $rs[$key] = stripslashes_array($arr[$key]); else $rs[$key] = htmlspecialchars(stripslashes($val),ENT_QUOTES);
   }
   return $rs;
}

?>
Return current item: The Referral Database