2015-04-05 124 views
-1

我有一张3列的表格。首先是学生的名字。第二栏是一个下拉菜单,其中应该选择从第一栏开始教这位学生的老师,这个下拉菜单中的其他值应该是其他老师,可以改变教师。第三栏是提交按钮 - 保存更改(如果你已经改变了这位学生的老师)。我用"selected=selected",但它在所有行只选择一个老师,不依赖于什么是db.Here的我的观点:如何从下拉菜单中显示选定的值

foreach ($students_show as $row) 
{ 

?> 
<tr> 
<td> 
    <?php echo $row->username; 
</td> 
<?php 
echo "<select name = 'add_teacher[]' >"; 
    foreach($teachers_show as $row) 
    { 

     ?> 
     <option value= '$row->username ' 
     <?php echo $row->username == $row->username ? 'selected="selected"' : '' ?>><?php echo $row->username ; ?></option> 


    <?php 
    } 
    echo "</select>"; 
    ?> 
    </td> 

我的模式是:

public function stdents_show() 
    { 
     $this->db->select('*'); 
     $this->db->from('users'); 
     $this->db->where('role_id', '2'); 
     $result=$this->db->get(); 
      return $result->result(); 
    } 
     public function teacher_show() 
    { 

     $this->db->select('teacher_id, users.username'); 
     $this->db->from('teacher_conn'); 
     $this->db->distinct('teacher_id'); 
     $this->db->join('users', 'users.user_id=teacher_conn.teacher_id'); 

     $result=$this->db->get(); 
      return $result->result(); 
    } 
+0

这是笨? – CMPS 2015-04-05 14:30:42

+0

是的,它是CodeIgniter。 – 2015-04-05 14:34:28

回答

1

您使用$row为学生和老师。 用这样的方式:

foreach ($students_show as $student) 

foreach($teachers_show as $teacher) 

和比较应该是这样的

<?php echo $teacher->username == $student->teahcer_name? 'selected="selected"' : '' ?>><?php echo $teacher->username ; ?></option> 
+0

我做到了:foreach($ students_show as $ student)和foreach($ teachers_show as $ teacher),但它们不相等:$ teacher-> username == $ student-> teahcer_name。它必须显示那个研究这个学生的老师。我的数据库表是:id,teacher_id,student_id。它们在一行中 - 就像在查询中的地方一样。但是如何在下拉菜单中使用它:$ this-> db-> where('teacher_id',$ this-> input-> post('teacher_id')); - 如果我在查询中使用它,则不会显示任何结果。 – 2015-04-05 20:57:15