I have these 2 while loops.
Loop. Left side screen. Shows users attached to a task.
$getcurrentusers = $conn->prepare("SELECT u.userid, u.username, u.Fname, u.inactive, r.userid, r.job_id FROM users AS u INNER JOIN job_responsible AS r ON u.userid = r.userid WHERE u.inactive = 0 AND r.job_id = $getPostID ORDER BY u.Fname"); $getcurrentusers->execute(); $resultgetcurrentusers = $getcurrentusers->get_result(); $getcurrentusers->close(); if ($resultgetcurrentusers->num_rows > 0) { while($row = $resultgetcurrentusers->fetch_assoc()) { echo $row["username"]." (".$row["Fname"].")<br />"; } } else { echo "Der er ikke valgt nogle ansvarlige til denne opgave."; }
Loop. Right side screen. List all possible users that can be added to the task
$getdepartmentview = $SetDepartmentView; $explodegetdepartmentview = explode(",",$getdepartmentview); $implodegetdepartmentview = "'".implode("','",$explodegetdepartmentview)."'"; $getusers = $conn->prepare("SELECT userid, username, Fname, inactive FROM users WHERE departmentid IN ($implodegetdepartmentview) AND inactive = 0 ORDER BY Fname"); $getusers->execute(); $resultgetusers = $getusers->get_result(); $getusers->close(); if ($resultgetusers->num_rows > 0) { while($row = $resultgetusers->fetch_assoc()) { echo "<input type=\"checkbox\" name=\"choose_responsible[]\" value=\"".$row["userid"].",\" />".$row["Fname"]."<br />"; } }
The problem is, I would really like, that the input checkbox is 'checked' on the right side when there is a match from the left side 1. loop.
So..
Loop 1 | Loop 2
name3 | name1
name5 | name2
name7 | name3 - Inputbox checked
name8 | name4
| name5 - Inputbox checked
| name6
| name7 - Inputbox checked
| name8 - Inputbox checked
| name9
| name10
CodePudding user response:
In the first loop you can store user_id in an array
$user_ids[] = $row["userid"]
In the second loop you check the current user id if it exists in the previous saved $user_ids array
$checked = false;
if (in_array($row["userid"], $user_ids)) {
$checked = true;
}
CodePudding user response:
This task calls for a LEFT JOIN in a single query to avoid multiple trips to the database and to avoid inefficient php processes on the result set.
$sql = <<<SQL
SELECT u.userid,
u.username,
u.Fname,
u.inactive,
r.userid responsibleUserId,
r.job_id
FROM users AS u
LEFT JOIN job_responsible AS r
ON u.userid = r.userid
AND r.job = ?
WHERE u.inactive = 0
ORDER BY u.Fname
SQL;
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $getPostID);
$result = $stmt->execute();
$users = $result->fetch_all(MYSQLI_ASSOC);
Now you have all of the data that you need and therefore no more reasons to visit the database.
To list the users who are assigned to the targeted job, you iterate the array of associative arrays and show users where job_id
is not null.
foreach ($users as $user) {
if ($user['job_id']) {
printf('%s (%s)<br />', $user['username'], $user['Fname']);
}
}
To display all users and conditionally check checboxes next to names that are already "responsible", iterate the array again and rely on job_id
to determine the checkbox.
foreach ($users as $user) {
printf(
'<input type="checkbox" name="choose_responsible[]" value="%d"%s/>%s<br />',
$user['userid'],
$user['job_id'] ? ' checked' : '',
$user['Fname']
);
}