"Japon","IDFRANCE"=>"France");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);Any idea ?Regards"> Autocompleter key and value - Forums

Forums

Autocompleter key and value

alec6w 14 Jul, 2015
Hi,

I followed the demo-autocompleter and it works.

In my case, my list of country is like this IDCOUNTRY => TEXTCOUNTRY :
Example
IDFRANCE => France
IDJAPAN => Japon
....

I don't know how to show the name of the country in my autocompleter and to retrieve the IDCOUNTRY in my forms like this :

Array
(
    [option] => com_chronoforms5
    [chronoform] => demo-autocompleter
    [event] => submit
    [country] => IDRANCE
    [button2] => Submit
)


I tried this but it doesn't work

$countries = array("IDJAPAN"=>"Japon","IDFRANCE"=>"France");
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);


Any idea ?
Regards
GreyHead 14 Jul, 2015
Hi alec6w,

I'm not quite sure what you are doing here. You want to look up the Country ID using the Country Name that has been saved from the AutoCompleter.

Presumably the AutoCompleter is looking up names from a database table so the simplest solution would be to use the name to look up the id something like this:
<?php
$db = \JFactory::getDBO();
$query = "
    SELECT `CountryID`
        FROM `#__country_table`
        WHERE `CountryName` = '{$form->data['country']}' ;
";
$form->data['CountryID'] = $db->loadResult();
?>
You'll need to add the correct table and column names.

Bob
alec6w 15 Jul, 2015
Hi

My countries are not in the database. They're in an array.
I want to show the name and retrieve the value like a dropdown.
In a dropdown, i show France but the value is IDFRANCE.

The user select France in the form, i would like to retrieve IDFRANCE after submit button.
GreyHead 15 Jul, 2015
Hi alec6w,

Re-load the array - you can include it from a separate file rather than write it twice; then look up the id from the country name.

Bob
alec6w 15 Jul, 2015
Hi

OK, i understand.
I hped i could do everything in the first part :?
Anyway, i'll add a custom code on submit event.
Thank you
alec6w 23 Jul, 2015
Hi,

I add what i did. It works and hope it couldp be usefull.
My table is like this :
ID : 14 / Valeur : France / Code : FRA

In the designer, add a field named country

Add an autocompleter event with ajax form event : countries_list
Add an event countries_list
Add a custom code in the event countries_list


<?php
$lang = JFactory::getLanguage();
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query = "SELECT `valeur` FROM `#__mycountries` WHERE `valeur` LIKE  '%{$form->data['country']}%' AND langue='{$lang->getTag()}' ;";
$db->setQuery($query);
$countries = $db->loadcolumn();

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


On submit event, add a custom code

$lang = JFactory::getLanguage();
$query = "SELECT `code` FROM `#__mycountries` WHERE `valeur` = '{$form->data['country']}' AND langue='{$lang->getTag()}' ;";
$db->setQuery($query);
$form->data['country'] = $db->loadResult();


That's all.
This topic is locked and no more replies can be posted.