Last active
December 11, 2015 17:48
-
-
Save kevinchampion/4637052 to your computer and use it in GitHub Desktop.
Mcard autocomplete
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Autocomplete ajax helper to retrieve autocomplete options from textual mcard | |
* discount search. | |
* | |
* @param $search | |
* User entered search string. | |
* | |
* @return | |
* JSON listing of autocomplete search options. | |
*/ | |
function mcard_search_autocomplete($search = '') { | |
$matches = $args = array(); | |
$searchsql = ''; | |
watchdog('mcard', 'autocomplete search query: !query !args', array('!query' => '<pre>' . print_r($search, TRUE) . '</pre>', '!args' => '<pre>' . print_r($_GET, TRUE) . '</pre>'), WATCHDOG_NOTICE); | |
// Generate search sql. | |
if($search) { | |
$searchfields = array("n.title" , "m.field_discount_long_description_value", "p.title", "q.body"); | |
// Break up into words and do a LIKE %..% search on each word. | |
$words = explode(' ', $search); | |
foreach($words AS $word) { | |
$searchsql .= isset($searchsql) ? ") AND (" : "AND (("; | |
$searchsql_parts = array(); | |
//search each field | |
foreach($searchfields as $searchfield) { | |
$searchsql_parts[] = $searchfield . " LIKE '%%%s%%'"; | |
$args[] = drupal_strtolower($word); | |
} | |
$searchsql .= implode(" OR ", $searchsql_parts); | |
} | |
$searchsql .= ")"; | |
} | |
else { | |
$searchsql = ')'; | |
} | |
// The base get all mcard discounts query | |
$query = "SELECT | |
DISTINCT n.nid, | |
n.title, | |
n.created, | |
m.field_discount_long_description_value, | |
p.title AS p_title, | |
p.created, | |
q.body, | |
p.nid AS pnid | |
FROM {node} n | |
INNER JOIN {content_type_mcard_discount} m on n.vid = m.vid | |
INNER JOIN {node} p on m.field_discount_provider_nid = p.nid | |
INNER JOIN {node_revisions} q on p.vid = q.vid | |
WHERE (n.type = 'mcard_discount' | |
AND n.status = 1 | |
" . $searchsql . " | |
ORDER BY p.title"; | |
watchdog('mcard', 'discount search query: !query !args', array('!query' => '<pre>' . print_r($query, TRUE) . '</pre>', '!args' => '<pre>' . print_r($args, TRUE) . '</pre>'), WATCHDOG_NOTICE); | |
$result = db_query_range($query, $args, 0, 10); | |
$searchresultshtml = ''; | |
$count = 0; | |
while ($row = db_fetch_object($result)) { | |
/*if ($search) { | |
$result = db_query_range("SELECT name FROM {users} WHERE LOWER(name) LIKE LOWER('%s%%')", $search, 0, 10); // only 10 results will show | |
while ($user = db_fetch_object($result)) { | |
$matches[$user->name] = check_plain($user->name); | |
} | |
}*/ | |
$matches[$row->p_title] = check_plain($row->p_title); | |
} | |
drupal_json($matches); //Returns the data in JSON format | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment