I want to get my code cleaner and more efficient. This code gets variables form a PHP file and filters it to show the selected user name, all available usergroups on a list box and the groups he is currently into another.
Currently it's working great but it has a lot of delay between the modal shows up and the groups being shown.
JS:
function saveUserGroup(id){
$('#useridgr').attr('placeholder','').val('');
$("#usernamegr").attr('placeholder', 'Nome').val('');
$('#list_box').html(''); $('#list_box_ini').html('');
$.ajax({
type: 'GET',
url: 'users/' + id + '/users' + '/id',
data: {'_token': $("input[name='_token']").val()},
success: function(data){
$('#useridgr').attr('placeholder',data.id).val(data.id);
$("#usernamegr").attr('placeholder',data.name).val(data.name);
loadgroupsdata(data.id, 0);
} }); }
var loadgroupsdata = (uid, type) => {
$.ajax({
type: 'GET',
url: 'usergrupo/' + uid + '/' + type,
data: {'_token': $("input[name='_token']").val()},
success: function(data){
if (type == 0) listbox = '#list_box';
else listbox = '#list_box_ini';
loadgroups(uid, listbox, type, data);
}
}) }
function loadgroups(uid, location, type, data){
$.each(data,function(i,v){
$(location).append("<option value='"+v.idgrupo+"'>"+v.nome+"</option>"); })
if(type == 0) loadgroupsdata(uid, 1);
}
function sendData(type){
uid = document.getElementById("useridgr").value;
if(type == 1) listname = 'list_box_ini'; else listname = 'list_box';
var e = document.getElementById(listname);
var value = e.options[e.selectedIndex].value;
$.ajax({
type: 'GET',
url: 'savegroup/' + uid + '/' + type + '/' + value,
data: {'_token': $("input[name='_token']").val()},
success: function(data){
saveUserGroup(uid);
}
})
}
PHP:
public function getData($uid, $tablename, $index)
{
$data = \DB::table($tablename)->where($index, $uid)->first();
return response()->json($data, 200);
}
public function getGroups($id, $type)
{
if ($type == 0){
$data = \DB::table('grupoutilizador')
-> join ('users', function ($join) use ($id){
$join->on('users.id', '=', 'grupoutilizador.iduser')
->where('users.id', "=", $id );
}) -> join ('grupo', 'grupo.idgrupo', "=", 'grupoutilizador.idgrupo') ->
select('users.id', 'users.name', 'grupo.idgrupo', 'grupo.nome')
-> get();
} else {
include 'config.php';
$usergroups = mysqli_query($conn2, 'SELECT grupo.idgrupo FROM grupo, grupoutilizador WHERE grupoutilizador.idgrupo = grupo.idgrupo AND grupoutilizador.iduser =' .$id);
$i = 0;
if ($usergroups->num_rows != 0){
foreach ($usergroups as $usergroup) {
$ids[$i] = $usergroup;
$i++;
}
} else {
$ids[$i] = 0-1;
}
$data = \DB::table('grupo')
->whereNotIn('grupo.idgrupo', $ids)
-> get();
}
return response()->json($data, 200);
}
public function saveGroups($uid, $type, $value){
if ($type == 1){
DB::table('grupoutilizador')->insert([
'iduser' => $uid,
'idgrupo' => $value,
]);
} else {
$result = DB::table('grupoutilizador') -> where([
['iduser', '=', $uid],
['idgrupo', '=', $value],
]) -> delete();
echo $result;
}
}
mysqli_query()
into your model? \$\endgroup\$