我必须向学生展示复选框,如果明确的老师教这些学生,他们必须被囚禁。如果这些学生不是他的学生,则不需要进行检查。我对copmare 2值有两个问题,但在我的情况下,当它们相同时,学生不会在ckechbox中检查,但会写成: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>
&#13;
我的模特是:
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;
}
}
}
}
&#13;
我的控制器是:
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);
}
}
&#13;
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;
}
}
}
&#13;
但是插入我已经拥有的行。我尝试用函数select_teachers_students()进行检查,如果它们在那里,我不想再次插入 - 我想更新它们。以及如何在选中复选框时为其设置值,以插入value="<?php echo $student->user_id; ?>"
但是,如果未选中复选框并按下提交按钮,则插入Value='0'
。如果他们不在那里,我想插入。
答案 0 :(得分:1)
似乎有一个额外的&gt;
之间?> >
<?php
标记。由于这是在foreach循环中,它将被执行多次。
更严重的是,您的视图逻辑需要整容。使用像Smarty这样的模板引擎。
答案 1 :(得分:1)
用于输出复选框的代码似乎不正确。请尝试以下方法:
<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>
这应该有效。在您的情况下,循环未正确设置...
答案 2 :(得分:1)
而不是使用:
<?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'";
}
因为错误而添加了一个&gt;最后。
答案 3 :(得分:0)
每个checked="checked"
元素只需要一个input
。然后,首先计算是否检查了学生,然后(当构建输出时)使用它。
<?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 );
}
?>