Kombinace checked a unchecked checkboxů v jednom checkboxlistu

ForestCZE
Člen | 209
+
0
-

Ahoj, jsem naprosto bezradný.

Mám tento kód:

$users = $this->connection->table('users');
$checked_users = $this->connection->fetchAll('SELECT users.id, username FROM users JOIN chat_permissions ON users.id = user_id WHERE chat_id = ?', $this->getParameter('id'));

$pole1 = [];
foreach($users as $user)
{
	if($user['id'] == $this->user->getIdentity()->id) continue;
	$pole1[$user['id']] = $user['username'];
}

$pole2 = [];
foreach($checked_users as $checked_user)
{
	if($checked_user['id'] == $this->user->getIdentity()->id) continue;
	$pole2[$checked_user['id']] = $checked_user['username'];
}

$form->addCheckboxList('allusers', ' ', $pole1)
     ->setRequired('Vyber alespoň jednoho uživatele.');

$form->addCheckboxList('checkedusers', ' ', $pole2)
     ->setRequired('Vyber alespoň jednoho uživatele.');

Jsem schopen nějak tyto dva listy spojit v jeden a zaškrtnout v něm to, co zaškrtnuté být má? Vážně mě nic nenapadá. Díky.

Editoval ForestCZE (20. 5. 2019 1:42)

h4kuna
Backer | 740
+
+1
-

Chceš, aby $pole1 byl seznam checkboxů a $pole2 zaškrtnuté checkboxy?

Tím pádem takto:

$pole2 = [];
foreach($checked_users as $checked_user)
{
    if($checked_user['id'] == $this->user->getIdentity()->id) continue;
    $pole2[] = $checked_user['id']; // je potřeba předat pole hodnot
}
$form->addCheckboxList('allusers', ' ', $pole1)
     ->setRequired('Vyber alespoň jednoho uživatele.')
	 ->setDefaultValue($pole2);

Editoval h4kuna (20. 5. 2019 9:03)