module.php
<?php
/**
* Implements hook_menu.
*/
function module_name_menu() {
$items['ajaxform_autocomplete_callback'] = array(
'page callback' => 'ajaxform_autocomplete_callback',
'type' => MENU_CALLBACK,
'access callback' => TRUE,
);
return $items;
}
/**
* Create Form.
*/
function module_name_form($form, &$form_state){
$form['article_search'] = array(
'#type' => 'textfield',
'#title' => 'Search Article',
'#maxlength' => 128,
'#autocomplete_path' => 'ajaxform_autocomplete_callback',
);
return $form;
}
function ajaxform_autocomplete_callback($text) {
$matches = array();
$items = db_query_range('SELECT n.nid, n.title, n.created FROM {node} n WHERE n.title LIKE :string', 0, 10, array('string'=>'%' . db_like($text). '%'));
foreach ($items as $item) {
$matches[$item->title] = $item->title . '( nid: ' . $item->nid .')';
}
drupal_json_output($matches);
}
?>
Output:-
Way2:- How to fetch the data in particular table using custom code in drupal 7
step 1:- We have a table and the table name is like 'Curd' we have some data in a table like:-
step 2:- Fetch the data in table Curd
Now Full code retrieve the data article and curd table...
module.php
<?php
/**
* Implements hook_menu.
*/
function module_name_menu() {
$items['ajaxform_autocomplete_callback'] = array(
'page callback' => 'ajaxform_autocomplete_callback',
'type' => MENU_CALLBACK,
'access callback' => TRUE,
);
return $items;
}
/**
* Create Form.
*/
function module_name_form($form, &$form_state){
$form['article_search'] = array(
'#type' => 'textfield',
'#title' => 'Search Article',
'#maxlength' => 128,
'#autocomplete_path' => 'ajaxform_autocomplete_callback',
);
return $form;
}
function ajaxform_autocomplete_callback($text) {
Use any of one code
1.Article data retrieve
$matches = array();
$items = db_query_range('SELECT n.nid, n.title, n.created FROM {node} n WHERE n.title LIKE :string', 0, 10, array('string'=>'%' . db_like($text). '%'));
foreach ($items as $item) {
$matches[$item->title] = $item->title . '( nid: ' . $item->nid .')';
}
drupal_json_output($matches);
2.This code use for custom table data retrieve
$results = array();
$query = db_select('curd', 'c');
$query
->condition('c.coupon_name', '%' . db_like($text) . '%', 'LIKE')
->fields('c', array('coupon_name'))
->orderBy('coupon_name', 'ASC');
$colors = $query->execute();
foreach ($colors as $row) {
$results[$row->coupon_name] = check_plain($row->coupon_name);
}
drupal_json_output($results);
}
?>
No comments:
Post a Comment
If you have any problem please let me know.