function tripal_autocomplete_organism
2.x tripal_organism.api.inc | tripal_autocomplete_organism($text) |
3.x tripal_chado.module.DEPRECATED.api.inc | tripal_autocomplete_organism($text) |
This function is intended to be used in autocomplete forms for searching for organisms that begin with the provided string
Parameters
$text: The string to search for
Return value
A json array of terms that begin with the provided string
Related topics
1 string reference to 'tripal_autocomplete_organism'
- tripal_organism_menu in tripal_organism/
tripal_organism.module - Implements hook_menu().
File
- tripal_organism/
api/ tripal_organism.api.inc, line 245 - Provides an application programming interface (API) to manage organisms
Code
function tripal_autocomplete_organism($text) {
$matches = array();
$genus = $text;
$species = '';
if (preg_match('/^(.*?) (.*)$/', $text, $matches)) {
$genus = $matches[1];
$species = $matches[2];
}
$sql = "SELECT * FROM {organism} WHERE lower(genus) like lower(:genus) ";
$args = array();
$args[':genus'] = $genus . '%';
if ($species) {
$sql .= "AND lower(species) like lower(:species) ";
$args[':species'] = $species . '%';
}
$sql .= "ORDER BY genus, species ";
$sql .= "LIMIT 25 OFFSET 0 ";
$results = chado_query($sql, $args);
$items = array();
foreach ($results as $organism) {
$name = $organism->genus . ' ' . $organism->species;
$items[$name] = $name;
}
drupal_json_output($items);
}