Location: PHPKode > scripts > Thesaurus tooltip > db_controller.php
<?PHP
/*
* Thesaurus
*
* @package Thesaurus
* @author $Author: sheiko $  
* @version $Id: controller.php, v 2.0 2007/06/05 15:58:15 sheiko Exp $ 
* @copyright (c) Dmitry Sheiko http://www.cmsdevelopment.com 
*/ 

/**
* Thesaurus Controller Class 
* @package Thesaurus
* @author $Author: sheiko $ 
*/

class Thesaurus {
	
	var $DataSourceFile;
	var $Output;
	var $BodyContent;
	var $Term;
	var $ErrorMsg;
	var $Length;
	var $TermList;
	
	/** 
    * Class Constructor
    * @return object
    */ 	
	
	function Thesaurus() {
		
		$db_info = array(
			"host" => "localhost",
			"user" => "root",
			"password" => "",
			"dbname" => "thesaurus",
		);
		
		$this->DataSourceFile = "sample.csv";
		$this->BodyContent = false;
		$this->ErrorMsg = false;
		$this->Term = false;
		$this->TermList = "";
		$this->Length = 0;
		$this->Output = '';		
		mysql_connect($db_info["host"], $db_info["user"], $db_info["password"])
        or $this->ErrorMsg = "Could not connect: " . mysql_error();
        mysql_select_db($db_info["dbname"])
        or $this->ErrorMsg = 'Can\'t use '.$db_info["dbname"].' : ' . mysql_error();

		if(isset($_POST["term"])) {
			$this->Term = urldecode($_POST["term"]);
			$this->Term = $this->str2lower($this->Term);
			$this->Term = trim($this->Term);
		}
		return $this;
	}
	/** 
    * Convert UTF-8 string 
    * 
    * @return boolean
    */ 	
	function str2lower($str) {
		if($_POST["casesensitive"]=="on") return $str;
		if(function_exists("mb_strtolower") )
			return  mb_strtolower($str, ((isset($_POST["charset"]) AND $_POST["charset"])?$_POST["charset"]:"UTF-8"));
		else 
			return strtolower($str); 		
	}
	
	
	
	/** 
    * Calc Click
    * 
    * @return boolean
    */ 		
	function calcClick() {
		if($_POST["casesensitive"]=="on") {
			mysql_query("UPDATE thesaurus SET clicked=clicked+1 WHERE term='{$this->Term}'");
		} else {
			mysql_query("UPDATE thesaurus SET clicked=clicked+1 WHERE LCASE(term)='{$this->Term}'");
		}
	}	
	
	/** 
    * Get data and filter data
    * 
    * @return boolean
    */ 		
	function getDataAndFitlering() {
		if($_POST["casesensitive"]=="on") {
			mysql_query("UPDATE thesaurus SET visited=visited+1 WHERE term='{$this->Term}'");
			$result = mysql_query("SELECT term, description FROM thesaurus WHERE term='{$this->Term}'");
		} else {
			mysql_query("UPDATE thesaurus SET visited=visited+1 WHERE LCASE(term)='{$this->Term}'");
			$result = mysql_query("SELECT term, description FROM thesaurus WHERE LCASE(term)='{$this->Term}'");
		}
		$fetch = mysql_fetch_array($result, MYSQL_ASSOC); 
		
		$this->Length = mysql_num_rows($result);		
		$this->Output = $fetch["description"];
	}
	
	/** 
    * Get data and parse body content
    * 
    * @return boolean
    */ 		
	function getDataAndParsing() {
		$result = mysql_query("SELECT term FROM thesaurus");
		$this->Length = mysql_num_rows($result);
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
       			$this->TermList .= "'{$row["term"]}',";
		}
		$this->TermList = preg_replace("/,$/", "", $this->TermList);
		
	}	

	/** 
    * Respond with JSON generation
    * 
    * @return boolean
    */ 		
	function execRespond() {
		if(isset($_POST["charset"]) AND $_POST["charset"])
			header("Content-type: text/html; charset={$_POST["charset"]}");
		else
			header("Content-type: text/html; charset=UTF-8");

		switch ($_POST["call"]) {
			case "getDataAndParsing":
				$this->getDataAndParsing();
				break;
			case "getDataAndFitlering":
				$this->getDataAndFitlering();
				break;				
			case "calcClick":
				$this->calcClick();
				break;					
		}
		
		if($this->Length==0) return false;
		$out = '{
		"thlength" : '.$this->Length.',
		"termlist" : ['.$this->TermList.'],
		"errormsg" : "'.$this->ErrorMsg.'",
		"content" : "'.addslashes($this->Output).'",
		"term" : "'.(isset($_POST["term"])?urldecode($_POST["term"]):'').'"
		}';
		print $out;
	}
	
}

$th = new Thesaurus();
$th->execRespond();
?>
Return current item: Thesaurus tooltip