【发布时间】:2019-11-28 06:45:08
【问题描述】:
【问题讨论】:
-
将您的代码作为文本而不是图像放在问题中。
-
您尝试过什么调试问题?为什么不在持久化之前过滤现有的复选框?
【问题讨论】:
从截图中我了解到,您不希望在同一部门的同一单位分配多个讲师。
所以在创建allocated 行之前,您需要检查这些条目是否已经存在。
$data = compact('course_id', 'department', 'unit');
// check the department and unit already assigned or not.
if(!allocated::where($data)->exists()){
$data['lecturers_name'] = $lecturers_name;
allocated::create($data);
}
【讨论】:
对于初学者,您没有提供任何代码示例。所以我后面的例子不会基于你的代码。
您可以禁用复选框,但如果有人真的想要,这很容易绕过。检查器可用于更改 html,以便启用禁用的复选框。当然,您可以在后端验证这一点。
我会选择仅显示具有其他人尚未选择的值的复选框。我的示例包含这两种解决方案。
// create connection with your database.
$conn = new mysqli($servername, $username, $password, $dbname);
// array with values that can be picked.
$values = ['Apple', 'Banana', 'Lemon'];
// loop trough the array.
foreach( $values as $fruit )
{
// select query -> check if someone else already choose the fruit.
$sql = "SELECT chosen_fruit FROM friends WHERE chosen_fruit = " . $fruit;
// execute the query.
$result = $conn->query($sql);
// if no results are found, meaning nobody else have picked this fruit yet.
if ($result->num_rows == 0) {
// enabled checkbox that is free to use
echo '<input type="checkbox" name="chosen_fruit" value="' . $fruit . '">'
} else {
// disabled checkbox because this fruit has already been chosen.
echo '<input type="checkbox" name="chosen_fruit" value="' . $fruit . '" disabled>'
}
}
编辑:我在发布答案后看到了您的编辑。
【讨论】: