Update Record Depend on Autocompleter

Yudhizth 20 Feb, 2017
Dear all,

How to update record depend on multiple autocompleter value? I have a table that will appear on autocompleter.

id_pertel | nomor | status

the status column will update to '1' if user choose nomor on autocompleter. If the only one number submitted, it will work, but if multiple number submitted, it won't work. picture on attach.
GreyHead 20 Feb, 2017
Hi Yudhizth ,

You have the values that you need as a comma separated string. You will need to use Custom Code to convert that to an array of entries that you can save to separate records.

Bob
Yudhizth 20 Feb, 2017
Thank you Bob,

I solved this problem with Custom Code to update the record.

Here's the code

<?php

$user =& JFactory::getUser();
$db =& JFactory::getDBO();
	
	
$str = $form->data['nomor'];
$exp = explode(",",$str);

//$text = implode(",", $exp);
$text = "'".implode("','", $exp)."'";



$query = "UPDATE `tbl_m_pertel` AS `update` SET `status` = '1' WHERE `update`.`nomor`  IN ($text)"; 

$db->setQuery($query); // add this line
$db->query(); // and this line

?> 


But, do you have any advice to convert that code into Custom Code and still using DB Save?
GreyHead 20 Feb, 2017
Hi Yudzith,

Something like this would let you use pertel as the Model ID in a DB Save action with Multiple save enabled:
<?php
$pertel = array();
$exp = explode(",", $form->data['nomor']);
foreach ( $exp as $k => $v ) {
  $pertel[$k] = array( 'nomor' => $v, 'status' = 1 ):
} 
$form->data['pertel'] = $pertel;
?>

Bob
Yudhizth 20 Feb, 2017
Thank you Bob,

Your code is works very well for inserting data, but not works when I put the condition inside DB Save

<?php
$str = $form->data['nomor'];
return array('nomor' => explode(",",$str));
?>



Array
(
    [6] => Array
        (
            [DB Save] => Array
                (
                    [Queries] => Array
                        (
                            [0] => UPDATE `tbl_m_pertel` AS `pertel` SET `nomor` = '91187', `status` = '1' WHERE `pertel`.`nomor` IN ('91187', '91185');
                            [1] => UPDATE `tbl_m_pertel` AS `pertel` SET `nomor` = '91185', `status` = '1' WHERE `pertel`.`nomor` IN ('91187', '91185');
                        )

                )

        )

)
Yudhizth 20 Feb, 2017
One more question,

can we set a maximum record on multiple autocompleter? on my screenshot above, i put two record, can we set the maximum record to five?
GreyHead 21 Feb, 2017
Hi Yudhizth,

I did not say that you could use that code inside a DB Save, add it in a Custom Code before the DB Save then "use pertel as the Model ID in a DB Save action with Multiple save enabled".

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