这里我有两张桌子。名称如下:rooms
和student_hostel
。
rooms
表如下所示:
id rm_number capacity bed_no class hostel is_vaccant
40 1 5 1A,1B,1C,1D,1E 27 7 1
41 2 4 2A,2B,2C,2D 28 7 0
42 3 3 3A,3B,3C 29 10 1
43 4 4 4A,4B,4C,4D 30 10 1
44 5 6 5A,5B,5C,5D,5E,5F 27 7 1
45 6 7 6A,6B,6C,6D,6E,6F,6G 29 10 1
student_hostel
表格如下所示:
id first_name stud_id hostel class room bed status
175 siraj WPGH00175 7 28 41 2A P
176 nesru WPGH00176 7 28 41 2B P
177 faizal WPGH00177 7 28 41 2C P
179 mashoor WPGH00179 7 28 41 2D G
180 dsf WPGH00180 7 27 40 1A G
181 ee WPGH00181 7 27 40 1B P
我希望获得所有状态' P'如同一种颜色和所有状态' G'一种颜色。 当我像这样使用我的代码时:
型号:
public function view_room_status($num,$offset)
{
$this->db->select('rooms.*,class.name as class_name,hostel.name as hostel_name,student_hostel.status,student_hostel.id as s_id');
$this->db->join('class','class.id=rooms.class');
$this->db->join('hostel','hostel.id=rooms.hostel');
$this->db->join('student_hostel','rooms.id=student_hostel.room','left');
$this->db->group_by('rooms.id');
$query=$this->db->get('rooms',$num,$offset)->result();
return $query;
}
查看如下:
<tbody>
<?php $n=1;
foreach($result as $row){
$tagsArray = explode(',', $row->bed_no);
$tagsLinksArray = array();?>
<tr>
<td><?php echo $n++; ?></td>
<td><?php echo ucfirst($row->rm_number)?></td>
<td><?php echo ucfirst($row->class_name)?></td>
<td><?php echo ucfirst($row->hostel_name)?></td>
<td>
<?php
foreach($tagsArray as $tag) {
if($row->status=='P')
{
// Remove spaces
$tagName = trim($tag);
$tagsLinksArray[]= '<a class="label label-success" href="view_room_occupied/'.$tagName.'">'.$tagName.'</a>';
}
elseif($row->status=='G')
{
// Remove spaces
$tagName = trim($tag);
$tagsLinksArray[]= '<a class="label label-primary" href="view_room_occupied/'.$tagName.'">'.$tagName.'</a>';
}
}
// Join links in a string
$tagsLinks = implode(',', $tagsLinksArray);
echo $tagsLinks;?>
</td>
</tr>
<?php } ?>
</tbody>
即使有其他状态,我得到的结果是数组中的所有值都变成一种颜色。像这样:
sl no room class hostel Bed
1 1 Periyar1 Periyar 1A,1B,1C,1D,1E
2 2 Periyar2 Periyar 2A,2B,2C,2D
3 3 Pamba1 Pamba
4 4 Pamba2 Pamba
5 5 Periyar1 Periyar
6 6 Pamba1 Pamba
这里&#39; 1A&#39;状态是&#39; P&#39;和&#39; 1B&#39;状态&#39;是&#39; G&#39;但是所有的1A,1B.1C,1D,1E和#39;在第二栏中显示相同的颜色和类似情况&#;; 2A,2B,2C&#39;状态是&#39; P&#39;和&#39; 2D&#39;状态是&#39; G&#39;但是为所有人获得相同的颜色。我想得到的结果是&#39; 2A,2B,2C&#39;应该是一种颜色,因为状态&#39; P&#39;和&#39; 2D&#39;应该是状态为&#39; G&#39;。
的另一种颜色