CFv5 - Autocompleter with DB table

montagne.rocciose 21 Jan, 2015
Hi all,
I read much more the forum but I cannot find anythink to solve my problem: I copied the 'demo-autocompleter' tutorial to modify it 'cause I need to read a db table to find the text typed into the TEXT BOX.

I explain much more: I hava a db-table with 3 fields

ID - AUTOINCREMENT
NAME - TEXT
ID_PROVINCIA - TEXT

the autocompler function is:
/******************/
$countries = array($form->data['Data']);
/****************/

foreach($countries as $country){
if(!empty($form->data['tag']) AND stripos($country, $form->data['tag']) === false){
continue;
}
$json[] = array('id' => $country, 'text' => $country);
}
echo json_encode($json);
/******************************************************************************/

The question is: how can I search into the db-table? The db-table is readed into the loader event and stored it into the 'Data' Model-ID. The script doesn't assign the db-table to the $coutry variable.
Excuse me for my English....πŸ™‚
Tanks to all,
M.R.
montagne.rocciose 01 Feb, 2015
1 Likes
Hi,
I solved this post. I hope it could be helpfull to all:

1. create the text field,
2. create and compile the autocompleter setup, into the loader event, usually.
3. Now you need to create a custom event with the same name used into the autocompleter setup (AJAX form event - field), Into this event you place a db-read witch reads the db-table (if you need it), you compile it and than you add a 'custom-code' setup whth a code similar to the following (an example)...
<?php
$countries = array();
if ( !$form->data['Data'] || count($form->data['Data']) < 1 ) {
  // no result was found
  $countries[] = 'Nessun comune in lista';
} else {
  foreach ( $form->data['Data'] as $d ) {
    $countries[$d['id']] =  $d['id'];
    $countries[$d['name']] =  $d['name'];
  }
}

foreach($countries as $country){
	if(!empty($form->data['tag']) AND stripos($country, $form->data['tag']) === false){
		continue;
	}
	$json[] = array('id' => $country, 'text' => $country);
}
echo json_encode($json);
?>

This script reads the db-table every time a user press a button into the field searching all db-fields wich contains the text...

Bye,
This topic is locked and no more replies can be posted.