Forums

CFv5 - Autocompleter with DB table

giusebos 11 Feb, 2016
Where do I place the db read?

right now my setup sheet is as follows:

in on load;
--autocompleter
--db read
--html render form

in on ajax_province
--custom code

my code:

    <?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);
    ?>


Unfortunately it does not work
GreyHead 11 Feb, 2016
Hi giusebos,

The DB Read action has to be in the same event - ajax_province - as the custom code to use the data being read. And the DB Read must be before the Custom Code action.

Bob
giusebos 11 Feb, 2016
maybe something wrong, meanwhile I wonder if this is the right order:
GreyHead 11 Feb, 2016
Hi giusebos,

That looks correct. What is the problem?

In you check the Network tab in your browser Web Developer tools you should be able to see what is being returned from the Ajax query.

Bob
giusebos 11 Feb, 2016
Answer
ok, I had a problem of adaptation of the code to my table


        <?php
        $lista_regioni = array();
        if ( !$form->data['Datareg'] || count($form->data['Datareg']) < 1 ) {
          // no result was found
          $lista_regioni[] = 'Nessuna regione nella lista';
        } else {
          foreach ( $form->data['Datareg'] as $d ) {
            $lista_regioni[$d['idRegione']] =  $d['idRegione'];
            $lista_regioni[$d['nomeRegione']] =  $d['nomeRegione'];
          }
        }
        foreach($lista_regioni as $una_regione){
         if(!empty($form->data['tag']) AND stripos($una_regione, $form->data['tag']) === false){
          continue;
         }
         $json[] = array('id' => $una_regione, 'text' => $una_regione);
        }
        echo json_encode($json);
        ?>


correct string $json[] = array('id' => $una_regione, 'text' => $una_regione);
wrong string $json[] = array('idregione' => $una_regione, 'text' => $una_regione);
giusebos 15 Feb, 2016
I wondered if it were possible, insert a new entry if it is not found
GreyHead 15 Feb, 2016
Hi giusebos,

You could possibly do this in the Ajax code box if you have the data to save. Or you could add custom code to let the User add the data themselves

BUT my experience is that if you let users add new data like this you can get a lot of 'junk' or near-duplicate entries.

Bob
giusebos 15 Feb, 2016
also considers that it is a private form, with its own specific operator.....

so it would be enough to put a db save inside On ajax_province, after db read?
This topic is locked and no more replies can be posted.