Autocompleter - search words that begin only with the letters entered

danielitob 02 Jan, 2017
Hi,
using autocompleter, i would search words that begin only with the letters entered and not all words that contains these letters. If is not possible, could i order the words having first ones starting with those letters and then words that contain those letters.

Regards
Daniele
GreyHead 02 Jan, 2017
Hi Daniele,

Yes you can do either of these - you need to edit the code in the AJAX event in your form so that the data is filtered/sorted before it is returned to the browser page.

Bob

PS I recall an earlier thread here where there was a hack needed to get data to sort in a particular way - but right now I can't find it and it may not apply to CFv5.
danielitob 02 Jan, 2017
Hi Bob,
thanks for your reply.

My code is:

<?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['nomeComune']] =  $d['nomeComune'];
    $countries[$d['idProvincia']] =  $d['idProvincia'];
  }
}
foreach($form->data['Data'] as $country){
 if(!empty($form->data['tag']) AND stripos( $country['nomeComune'], $form->data['tag']) === false ){
  continue;
 }
$json[] = array('id' => $country['idProvincia'], 'text' => $country['nomeComune']);
}
echo json_encode($json);
?>


I should modify the red code in IF statement? (stripos($country['nomeComune'], $form->data['tag']) === false)

Regards
Daniele
GreyHead 04 Jan, 2017
Hi Daniele,

Yes you'd change that code. At present the 'matching' code is
stripos( $country['nomeComune'], $form->data['tag']) === false )
If you check the PHP docs here you'll see that strpos() returns the start position, where 0 is the beginning of the string - so
. . . stripos( $country['nomeComune'], $form->data['tag']) === false OR stripos( $country['nomeComune'], $form->data['tag']) > 0 
should only check for countries where the tag is found at the start of the country name.

Bob
danielitob 04 Jan, 2017
Hi Bob,
thanks a lot. Now it works correctly!!!

have a nice day

Regards
Daniele
This topic is locked and no more replies can be posted.