Skip to content

Instantly share code, notes, and snippets.

@mebjas
Created September 1, 2015 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mebjas/517bb28152bfce585552 to your computer and use it in GitHub Desktop.
Save mebjas/517bb28152bfce585552 to your computer and use it in GitHub Desktop.
<?php
// ..... all other code ---
// Assuming database is connected
$k = $_POST['search_key'];
// Assuming $k is the search key
//include __DIR__ .'<path to lib.search.php>'
// i.e include the search library here
include __DIR__ .'/lib.search.php';
$s = new msearch($k);
$result = $s->getResult();
// This will have the search result
<?php
/**
* Code to search though the database against a key
* and get result in sorted order
*
* Author: Minhaz A V <minhazav@gmail.com>
*/
// To avoid multiple redeclarations
if (!defined('_LIB_SEARCH')) {
define('_LIB_SEARCH', 'defined');
// Assumes database is connected
class msearch {
// --------------------------------------------------------------
// --- class variables here ---
// --------------------------------------------------------------
// The search key used
public $key = '';
public $Ki, $Wi;
// General things
public $_general_terms = array('dr', 'in', 'at', 'from', 'on', ',', '.', 'doctor');
// Categories of entities
public $_categories = array('name', 'location', 'specialisation', 'pincode', 'general');
// Current default weightage to each category
public $_d_priority = array(1.1, 1, 1, 1, 0);
// priority alloted to each in runtime
public $_priority = array(1.1, 1, 1, 1, 0);
// Doctors
public $Di = array();
public $Di_m = array();
// --------------------------------------------------------------
// --- member functions from here ---
// --------------------------------------------------------------
// constructor
public function __construct($key) {
$this->key = self::itrim(strtolower($key));
// Split the key by space
$this->Ki = self::mysql_filter(explode(' ', $this->key));
// weightage array
$this->Wi = array();
$this->gather();
}
private function gather() {
// Identify the category for each Ki
foreach ($this->Ki as $key => $value) {
$this->Wi[] = array(0, 0, 0, 0, 0);
if (in_array($value, $this->_general_terms)) {
// only possiblity its a general term
$this->Wi[$key][4] = 1;
if ($value == 'dr' || $value == 'doctor') {
$this->_priority = $this->_d_priority;
$this->_priority[0] = $this->_d_priority[0] + .5;
} else if ($value == 'from') {
$this->_priority = $this->_d_priority;
$this->_priority[1] = $this->_d_priority[1] + .5;
}
} else {
if (is_numeric($value) && strlen($value) == 6) {
// only possiblity pincode
$query = mysql_query("SELECT id FROM `users` WHERE `type` = 'DOCTOR' AND `pin` LIKE '%$value%'");
$has_result = false;
while ($row = mysql_fetch_array($query)) {
if (!isset($this->Di[$row['id']])) {
$this->Di[$row['id']] = $this->_priority[3];
$this->Di_m[$row['id']] = array(0,0,0,1,0);
}
else {
$this->Di[$row['id']] += $this->_priority[3];
$this->Di_m[$row['id']][3] = 1;
}
$has_result = true;
}
$this->Wi[$key][3] = ($has_result) ? 1 : 0;
} else {
// Now get count and get weightage for different things
// name
$query = mysql_query("SELECT id FROM `users` WHERE `type` = 'DOCTOR' AND `name` LIKE '%$value%'");
$has_result = false;
while ($row = mysql_fetch_array($query)) {
if (!isset($this->Di[$row['id']])) {
$this->Di[$row['id']] = $this->_priority[0];
$this->Di_m[$row['id']] = array(1,0,0,0,0);
}
else {
$this->Di[$row['id']] += $this->_priority[0];
$this->Di_m[$row['id']][0] = 1;
}
$has_result = true;
}
$this->Wi[$key][0] = ($has_result) ? 1 : 0;
// location
$query = mysql_query("SELECT id FROM `users` WHERE `type` = 'DOCTOR' AND
(`addrline1` LIKE '%$value%' OR `addrline2` LIKE '%$value%' OR
`city` LIKE '%$value%' OR `state` LIKE '%$value%')");
$has_result = false;
while ($row = mysql_fetch_array($query)) {
if (!isset($this->Di[$row['id']])) {
$this->Di[$row['id']] = $this->_priority[1];
$this->Di_m[$row['id']] = array(0,1,0,0,0);
}
else {
$this->Di[$row['id']] += $this->_priority[1];
$this->Di_m[$row['id']][1] = 1;
}
$has_result = true;
}
$this->Wi[$key][1] = ($has_result) ? 1 : 0;
// TODO: specialisation
}
}
$sum = array_sum($this->Wi[$key]);
foreach ($this->Wi[$key] as $k => $v) {
$this->Wi[$key][$k] = ($sum != 0) ? intval($v) / intval($sum) : 0;
}
}
}
public function getResult() {
// Sort result by score
arsort($this->Di);
$result = array();
foreach ($this->Di as $key => $value) {
$query = mysql_query("SELECT `name`, addrline1, addrline2, city, state, pin FROM `users` WHERE type = 'DOCTOR' AND id = '$key'; ");
if (!mysql_num_rows($query)) continue;
$row = mysql_fetch_array($query);
$t = array(
'key' => $key,
'score' => $value,
'name' => $row['name'],
'addrline1' => $row['addrline1'],
'addrline2' => $row['addrline2'],
'city' => $row['city'],
'state' => $row['state'],
'pin' => $row['pin']
);
$result[] = $t;
}
return $result;
}
// --------------------------------------------------------------
// --- static methods from here ---
// --------------------------------------------------------------
// Function to remove multiple consecutive spaces from search key
private static function itrim($k) {
$k = trim($k);
$len = strlen($k);
$j = 1;
for ($i = 1; $i < $len; $i++) {
if ($k[$i] == ' ' && $k[$i-1] == ' ') {
continue;
} else {
$k[$j++] = $k[$i];
}
}
$k[$j] = '\0';
return substr($k, 0, $j);
}
// Filter each element of array from mysql injection attack
private static function mysql_filter($Ki) {
$t = array();
foreach ($Ki as $key => $value) {
$t[] = mysql_real_escape_string($value);
}
return $t;
}
};
}
array (size=249)
0 =>
array (size=8)
'key' => int 5919
'score' => float 4.1
'name' => string 'RAVINDRA KUMAR GUPTA' (length=20)
'addrline1' => string 'RAVINDRA HOSPITAL AND HEART CENTRE' (length=34)
'addrline2' => null
'city' => string 'HISAR' (length=5)
'state' => string 'haryana' (length=7)
'pin' => string '125001' (length=6)
1 =>
array (size=8)
'key' => int 2591
'score' => float 3.1
'name' => string 'Ravi Ghai' (length=10)
'addrline1' => string 'H.No. 529 Sector 2 ' (length=19)
'addrline2' => null
'city' => string 'Panchkula' (length=9)
'state' => string 'Haryana' (length=7)
'pin' => string '134109' (length=6)
2 =>
array (size=8)
'key' => int 3664
'score' => float 3.1
'name' => string 'Ravi Kant Bhisham' (length=17)
'addrline1' => string 'Bhisham Hospital ' (length=17)
'addrline2' => null
'city' => string 'Bhiwani' (length=7)
'state' => string 'Haryana' (length=7)
'pin' => string '127021' (length=6)
3 =>
array (size=8)
'key' => int 4145
'score' => float 2.6
'name' => string 'Ravindranath Tagore Nallamotho' (length=30)
'addrline1' => string 'Ravi Cardiac Diabetic Care Centre' (length=33)
'addrline2' => null
'city' => string 'Ponnur' (length=6)
'state' => string 'Andhra Pradesh' (length=14)
'pin' => string '522124' (length=6)
4 =>
array (size=8)
'key' => int 1921
'score' => float 2.6
'name' => string 'Pravin Lahuji Nitnavare' (length=23)
'addrline1' => string '"PRAVIN L NITNAVARE' (length=19)
'addrline2' => null
'city' => string ' DR. AMBEDKAR ROAD' (length=18)
'state' => string 'NAGAUR' (length=6)
'pin' => string ' INDORA"' (length=8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment