我正在使用PHP和MYSQL在Yii2中创建一个占用/空置的房间图表。 我能够得到我想要的结果,但我正在做的不是正确的方法,因为我正在硬编码。 我想要的是使它充满活力。
我的查询是这样的:
command = Yii::$app->db->createCommand
("select a.room_category, group_concat(b.room_name) vacant_beds
from (select distinct id, room_category from room_category) a
left join
(select rct.room_category AS room_category,
rn.room_name
from room_category rct
left join room_name rn on rn.room_category = rct.id
left join patient_detail pd on rn.id = pd.bed_type
and (isnull(pd.discharge_date) or now() between pd.admission_date and pd.discharge_date)
where isnull(pd.id)
order by rct.room_category, rn.room_name) b on a.room_category=b.room_category
group by a.room_category
order by a.id");
$rows= $command->queryAll();
$rows
的结果是这样的:
array (size=9)
0 =>
array (size=2)
'room_category' => string 'MALE GENERAL WARD' (length=17)
'vacant_beds' => string 'MG-8,MG-2,MG-4,MG-6,MG-7' (length=24)
1 =>
array (size=2)
'room_category' => string 'FEMALE GENERAL WARD' (length=19)
'vacant_beds' => string 'FG-4,FG-1,FG-2,FG-3' (length=19)
2 =>
array (size=2)
'room_category' => string 'MOTHER CHILD WARD' (length=17)
'vacant_beds' => string 'MC-2,MC-4,MC-5,MC-6' (length=19)
3 =>
array (size=2)
'room_category' => string 'TWIN' (length=4)
'vacant_beds' => string 'TW-A1,TW-A2,TW-B2,TW-C1,TW-C2' (length=29)
4 =>
array (size=2)
'room_category' => string 'NICU' (length=4)
'vacant_beds' => string 'NICU-6,NICU-1,NICU-7,NICU-3,NICU-8,NICU-4,NICU-5' (length=48)
5 =>
array (size=2)
'room_category' => string 'CLASSIC' (length=7)
'vacant_beds' => string 'CL-6,CL-8,CL-4,CL-5' (length=19)
6 =>
array (size=2)
'room_category' => string 'DELUXE' (length=6)
'vacant_beds' => string 'DLX-5,DLX-6' (length=11)
7 =>
array (size=2)
'room_category' => string 'EXECUTIVE' (length=9)
'vacant_beds' => null
8 =>
array (size=2)
'room_category' => string 'AC GENERAL WARD' (length=15)
'vacant_beds' => string 'AG-5,AG-1,AG-2,AG-3,AG-4' (length=24)
然后对于每个房间,我这样做:
$commandOccupiedmg1 = Yii::$app->db->createCommand
("SELECT pd.patient_name as name,i.ipd_patient_id ipd,i.care_of_name relation,
i.mobile, i.district_city,pd.admission_date from patient_detail pd,
ipd_patient_entry i, room_name rn
where pd.ipd_patient_id=i.id and discharged !=1 and rn.id=pd.bed_type and rn.room_name='MG-1'");
$mg1s= $commandOccupiedmg1->queryAll();
mg1s的数据如下:
array (size=1)
0 =>
array (size=6)
'name' => string 'SAROJ MIRDHA' (length=12)
'ipd' => string '0749/15' (length=7)
'relation' => string 'GUDUM MIRDHA' (length=12)
'mobile' => string '7381566425' (length=10)
'district_city' => string 'SAMBALPUR' (length=9)
'admission_date' => string '2015-06-16 21:45:00' (length=19)
并访问我正在执行此查询的值:
foreach($mg1s as $mg1){
$mg1_patient_name=$mg1['name'];
$mg1_mobile =$mg1['mobile'];
$mg1_ipd = $mg1['ipd'];
$mg1_relation = $mg1['relation'];
$mg1_admission_date=$mg1['admission_date'];
}
然后显示我正在使用此代码的每个房间的信息。为每个房间都这样做。
<table class="table table-striped table-bordered discharge-note-border">
<tr>
<td>MALE</td>
<?php
if (array_key_exists('0', $rows) && strpos($rows[0]['vacant_beds'],'MG-1')!==FALSE) {
echo "<td style='background-color:#FFFFCC;'/>MG-1 </td>";
}else{
echo "<td style='background-color:#CCFFFF;'>
<div class='occupied'>MG-1<div id='occupied-hover'>
Patient Name - $mg1_patient_name <br> IPD No. - $mg1_ipd
<br>
Guardian - $mg1_relation <br> Mobile - $mg1_mobile <br>
Admission Date - $mg1_admission_date
</div></div> </td>";
}
问题是大约有50个房间。我很难编码一切。如果添加或删除任何房间,我必须更改代码或它将中断。
需要建议我如何以更好的方式动态地完成它。
答案 0 :(得分:1)
如果您使用的是Yii2,我建议您将yii\db\ActiveRecord
与yii\db\Query
结合使用。
您可以使用Gii生成从ActiveRecord
延伸的模型类。它会生成可与Lazy Loading and/or Eager Loading一起使用的关系。
此外,通过使用这些,您可以使用更易于阅读和更动态的代码构建查询。此外,您实际上可以使用模型类中具有自己的方法和未存储在数据库中的额外属性的对象。
答案 1 :(得分:0)
在像这样的关系查找中,有几个数据库函数在构建输出时非常有用,可以让你成为动态的。
我对Yii不是很熟悉,但通常你会想要使用工作流程来执行:
//QUERY ALL ROOMS
$rooms = mysqli_query($rooms_query)
$rooms_count = myqsli_num_rows($rooms);
for($r = 0; $rooms_count < $r; $r++)
{
//GET DATA AND STEP TO NEXT RECORD
$rooms_details = mysqli_fetch_assoc($rooms)
//STEP THROUGH EACH ROOM AND ASK THE DB FOR PERTINENT INFORMATION
$vacancies_sql = "SELECT vacant_beds FROM (vacancies) WHERE room_id = $rooms_details['id']";
$vacancies = mylsqli_query($vancancies_sql);
$vacancies_count = mysqli_num_rows($vacancies);
//GO THROUGH EACH VANCANY RECORD, AND ADD IT, AND IT's ROOM TO OUTPUT
$output[$r] = array('room' => $rooms_details['room_name']);
for($v=0;$v < $vacancies_count; $v++)
{
$vacancies_details = mysqli_fetch_assoc($vacancies);
$output[$r]['vacancies'] = $vancancies_details['vacant_bed_id'];
}
return $output;
因此,这将为您提供所需细节的多维度。结构如:
array [0] =&gt; (room ='MALE',空缺=数组(0 ='mg1'等等)
因此,要了解这看起来像什么,您可以尝试print_r($ output)
显然,这不是完整的工作代码。它是伪造的代码,用于显示将数据按压到有用的多维数组中的想法和方法,您可以在视图中轻松使用它。
希望这有帮助!如果您希望我进一步解释或澄清我很乐意更新我的答案,请在评论中告诉我。