如何使某些条件,复选框被选中
How to make if some condition, checkbox to be checked
我必须向学生展示复选框,如果确定的老师教这些学生,他们必须被勾选。如果这些学生不是他的学生,那就不用查了。我对 copmare 2 值有两个查询,但在我的情况下,当它们相等时,学生不会在 ckechboxes 中选中,但它是这样写的:checked="checked" > >
。以及如何关闭我的复选框,它显示在学生姓名前 > > >
。
我的看法是:
<?php
foreach($select_students as $student)
{
?>
<tr><td class='col-md-2'>
<input type="checkbox" name="student[]" value="<?php echo $student->user_id; ?>"
<?php foreach($select_teachers_students as $select) {
echo $student->user_id == $select->student_id ? 'checked="checked"' : ''
; ?> >
<?php }
echo $student->username; ?>
</td><td>
我的模型是:
public function select_students() {
$this->db->select('users.user_id, users.username, users.school_id, users.class, users.division, users.role_id, schools.school_name, schools.region,class_divisions.division');
$this->db->from('users');
$this->db->join('class_divisions','class_divisions.id=users.division','left');
$this->db->join('schools','schools.school_id=users.school_id','left');
$this->db->where('(users.deactivated_at = "0000-00-00 00:00:00" OR users.deactivated_at IS NULL) AND users.role_id = 1 ');
$this->db->where('users.school_id',$this->uri->segment(4) );
$result=$this->db->get();
return $result->result();
}
public function edit_students() {
$submit=$this->input->post('change');
$date = new DateTime("now");
if(isset($submit)&&($submit=='Промени ученици')) {
if($this->input->post('student')) {
foreach($this->input->post('student') AS $student) {
$data = array(
'student_id' => $student,
// 'teacher_id' => $this->uri->segment(3),
'created_at' => $date->format('Y-m-d H:i:s')
);
$this->db->where('teacher_id',$this->uri->segment(3));
$this->db->update('teacher_student_conn', $data);
if($this->db->affected_rows() > 0)
{
return true;
}
return false;
}
}
}
}
我的控制器是:
public function edit_students()
{
if($this->admin_model->edit_students()) {
echo "Tou changed teacher successfully!";
$teacher_id = $this->uri->segment(3);
$school_id = $this->uri->segment(4);
header('Refresh: 2; url=/survey/index.php/admin/edit_students/'.$teacher_id .'/' .$school_id);
}
else
{
$data['dynamic_view'] = 'edit_students';
$data['select_students']=$this->admin_model->select_students();
$data['select_teachers_students']=$this->admin_model->select_teachers_students();
$data['teacher_name']=$this->admin_model->teacher_name();
$data['menu']=$this->menu_model->get_menu();
$this->load->view('templates/main',$data);
}
}
现在我编辑了我的模型,它是:
public function select_teachers_students() {
$this->db->select('*');
$this->db->from('teacher_student_conn');
$this->db->where('teacher_student_conn.teacher_id',$this->uri->segment(3) );
$result=$this->db->get();
return $result->result();
}
public function edit_students() {
$submit=$this->input->post('change');
$date = new DateTime("now");
if(isset($submit)&&($submit=='Промени ученици')) {
if($this->input->post('student')) {
foreach($this->input->post('student') AS $student) {
if($this->input->post('student')==$this->select_teachers_students()) {
$data = array(
'student_id' => $student,
'created_at' => $date->format('Y-m-d H:i:s')
);
$this->db->where('teacher_id',$this->uri->segment(3));
$this->db->update('teacher_student_conn', $data);
}
else if($this->input->post('student')!=$this->select_teachers_students()) {
$data = array(
'student_id' => $student,
'teacher_id' => $this->uri->segment(3),
'created_at' => $date->format('Y-m-d H:i:s')
);
$this->db->insert('teacher_student_conn', $data);
}
}
if($this->db->affected_rows() > 0)
{
return true;
}
return false;
}
}
}
但会插入我已有的行。我尝试使用函数 select_teachers_students() 进行检查,如果它们存在,我不想再次插入 - 我想更新它们。以及如何在复选框被选中时设置值,以插入 value="<?php echo $student->user_id; ?>"
但是当复选框未被选中时,我按下提交按钮,以插入 Value='0'
。如果它们不存在,我想插入。
你的
之间似乎有一个额外的 >
?> >
<?php
标签。由于这是在 foreach 循环内,因此它将被执行多次。
更严重的是,您的视图逻辑需要整容。使用像 Smarty 这样的模板引擎。
用于输出复选框的代码似乎不正确。请尝试以下操作:
<td class='col-md-2'>
<input type="checkbox" name="student[]" value="<?php echo $student->user_id; ?>"
<?php foreach($select_teachers_students as $select) { ?>
<?php echo $student->user_id == $select->student_id ? 'checked="checked"' : ''; ?>
<?php } ?>
/>
<?php echo $student->username; ?>
</td>
这应该有效。在您的情况下,循环设置不正确...
而不是使用:
<?php foreach($select_teachers_students as $select) {
echo $student->user_id == $select->student_id ? 'checked="checked"' : ''
; ?> >
您应该使用 in_array 功能来检查老师是否教过任何学生。
示例:
select_teachers_students=array(12, 34, 25);// Student ids array
if(in_array($student,select_teachers_students){
echo "checked='checked'";
}
因为错误在末尾加了一个>
每个 input
元素只需要一个 checked="checked"
。然后,首先计算学生是否被检查,然后(当构建输出时)使用它。
<?php
foreach ( $select_students as $student ) {
$checked = false;
foreach ( $select_teachers_students as $select ) {
if ( $student->user_id == $select->student_id ) {
$checked = true;
break;
}
}
printf( '<tr><td class="col-md-2">'
. '<input type="checkbox" name="student[]" value="%s" %s />'
. '</td><td>%s</td></tr>',
$student->user_id,
$checked ? 'checked="checked"' : '',
$student->username );
}
?>
我必须向学生展示复选框,如果确定的老师教这些学生,他们必须被勾选。如果这些学生不是他的学生,那就不用查了。我对 copmare 2 值有两个查询,但在我的情况下,当它们相等时,学生不会在 ckechboxes 中选中,但它是这样写的:checked="checked" > >
。以及如何关闭我的复选框,它显示在学生姓名前 > > >
。
我的看法是:
<?php
foreach($select_students as $student)
{
?>
<tr><td class='col-md-2'>
<input type="checkbox" name="student[]" value="<?php echo $student->user_id; ?>"
<?php foreach($select_teachers_students as $select) {
echo $student->user_id == $select->student_id ? 'checked="checked"' : ''
; ?> >
<?php }
echo $student->username; ?>
</td><td>
我的模型是:
public function select_students() {
$this->db->select('users.user_id, users.username, users.school_id, users.class, users.division, users.role_id, schools.school_name, schools.region,class_divisions.division');
$this->db->from('users');
$this->db->join('class_divisions','class_divisions.id=users.division','left');
$this->db->join('schools','schools.school_id=users.school_id','left');
$this->db->where('(users.deactivated_at = "0000-00-00 00:00:00" OR users.deactivated_at IS NULL) AND users.role_id = 1 ');
$this->db->where('users.school_id',$this->uri->segment(4) );
$result=$this->db->get();
return $result->result();
}
public function edit_students() {
$submit=$this->input->post('change');
$date = new DateTime("now");
if(isset($submit)&&($submit=='Промени ученици')) {
if($this->input->post('student')) {
foreach($this->input->post('student') AS $student) {
$data = array(
'student_id' => $student,
// 'teacher_id' => $this->uri->segment(3),
'created_at' => $date->format('Y-m-d H:i:s')
);
$this->db->where('teacher_id',$this->uri->segment(3));
$this->db->update('teacher_student_conn', $data);
if($this->db->affected_rows() > 0)
{
return true;
}
return false;
}
}
}
}
我的控制器是:
public function edit_students()
{
if($this->admin_model->edit_students()) {
echo "Tou changed teacher successfully!";
$teacher_id = $this->uri->segment(3);
$school_id = $this->uri->segment(4);
header('Refresh: 2; url=/survey/index.php/admin/edit_students/'.$teacher_id .'/' .$school_id);
}
else
{
$data['dynamic_view'] = 'edit_students';
$data['select_students']=$this->admin_model->select_students();
$data['select_teachers_students']=$this->admin_model->select_teachers_students();
$data['teacher_name']=$this->admin_model->teacher_name();
$data['menu']=$this->menu_model->get_menu();
$this->load->view('templates/main',$data);
}
}
public function select_teachers_students() {
$this->db->select('*');
$this->db->from('teacher_student_conn');
$this->db->where('teacher_student_conn.teacher_id',$this->uri->segment(3) );
$result=$this->db->get();
return $result->result();
}
public function edit_students() {
$submit=$this->input->post('change');
$date = new DateTime("now");
if(isset($submit)&&($submit=='Промени ученици')) {
if($this->input->post('student')) {
foreach($this->input->post('student') AS $student) {
if($this->input->post('student')==$this->select_teachers_students()) {
$data = array(
'student_id' => $student,
'created_at' => $date->format('Y-m-d H:i:s')
);
$this->db->where('teacher_id',$this->uri->segment(3));
$this->db->update('teacher_student_conn', $data);
}
else if($this->input->post('student')!=$this->select_teachers_students()) {
$data = array(
'student_id' => $student,
'teacher_id' => $this->uri->segment(3),
'created_at' => $date->format('Y-m-d H:i:s')
);
$this->db->insert('teacher_student_conn', $data);
}
}
if($this->db->affected_rows() > 0)
{
return true;
}
return false;
}
}
}
但会插入我已有的行。我尝试使用函数 select_teachers_students() 进行检查,如果它们存在,我不想再次插入 - 我想更新它们。以及如何在复选框被选中时设置值,以插入 value="<?php echo $student->user_id; ?>"
但是当复选框未被选中时,我按下提交按钮,以插入 Value='0'
。如果它们不存在,我想插入。
你的
之间似乎有一个额外的 >?> >
<?php
标签。由于这是在 foreach 循环内,因此它将被执行多次。
更严重的是,您的视图逻辑需要整容。使用像 Smarty 这样的模板引擎。
用于输出复选框的代码似乎不正确。请尝试以下操作:
<td class='col-md-2'>
<input type="checkbox" name="student[]" value="<?php echo $student->user_id; ?>"
<?php foreach($select_teachers_students as $select) { ?>
<?php echo $student->user_id == $select->student_id ? 'checked="checked"' : ''; ?>
<?php } ?>
/>
<?php echo $student->username; ?>
</td>
这应该有效。在您的情况下,循环设置不正确...
而不是使用:
<?php foreach($select_teachers_students as $select) {
echo $student->user_id == $select->student_id ? 'checked="checked"' : ''
; ?> >
您应该使用 in_array 功能来检查老师是否教过任何学生。 示例:
select_teachers_students=array(12, 34, 25);// Student ids array
if(in_array($student,select_teachers_students){
echo "checked='checked'";
}
因为错误在末尾加了一个>
每个 input
元素只需要一个 checked="checked"
。然后,首先计算学生是否被检查,然后(当构建输出时)使用它。
<?php
foreach ( $select_students as $student ) {
$checked = false;
foreach ( $select_teachers_students as $select ) {
if ( $student->user_id == $select->student_id ) {
$checked = true;
break;
}
}
printf( '<tr><td class="col-md-2">'
. '<input type="checkbox" name="student[]" value="%s" %s />'
. '</td><td>%s</td></tr>',
$student->user_id,
$checked ? 'checked="checked"' : '',
$student->username );
}
?>