我有两张名为 tbl_collectiondesireddatetimelocations 和 tbl_locations
的表格tbl_collectiondesireddatetimelocations 的结构是:
pk_collectiondesireddatetimelocationid
, fk_tbl_locations_locationid, fromdate, todate, fromtime, totime
tbl_locations 的结构是
pk_locationid, name
我有以下页面:
在第一列中,从tbl_locations表填充位置。
我想要的是填写所请求列的数据?将从tbl_collectiondesireddatetimelocations中检索数据。对于例如班加罗尔的位置ID是1.我将检查tbl_collectiondesireddatetimelocations fk_tbl_locations_locationid列中是否存在位置ID 1。如果它存在,它将检索fromdate,todate,fromtime,totime列值并填充该特定位置的数据。
我写了以下代码:
<table style="width:100%;margin:0px;" class="maintable">
<tr><td colspan="1"></td><td colspan="4">
<font style="font-weight:bold;">Requested</font></td>
<td colspan="4"><font style="font-weight:bold;">Assigned</font></td>
</tr><tr>
<td>Location</td>
<td>From Date</td>
<td>To Date</td>
<td>From Time</td>
<td>To Time</td>
<td>From Date/td>
<td>To Date</td>
<td>From Time</td>
<td>To Time</td></tr>
<?php
foreach($locations as $location)
{
foreach($desireddatetimelocations as $desireddatetimelocation)
{
if($desireddatetimelocation['fk_tbl_locations_locationid']==
$location['pk_locationid'])
{
$fromdate=$desireddatetimelocation['fromdate'];
$todate=$desireddatetimelocation['todate'];
$fromtime=$desireddatetimelocation['fromtime'];
$totime=$desireddatetimelocation['totime'];
}
}
?>
<tr><td><?php echo $location['name'];?></td>
<td><input type="text" name="txtFromDate_
<?php echo $location['pk_locationid'];?>" class="field" style="width:80px;"
readonly="" value="<?php echo $fromdate;?>"/></td>
<td><input type="text" name="txtToDate_
<?php echo $location['pk_locationid'];?>"
class="field" style="width:80px;" readonly="" value="<?php echo $todate;?>"/></td>
<td><input type="text" name="txtFromTime_
<?php echo $location['pk_locationid'];?>"
class="field" style="width:80px;" readonly="" value="<?php echo $fromtime;?>"/></td>
<td><input type="text" name="txtToTime_
<?php echo $location['pk_locationid'];?>"
class="field" style="width:80px;" readonly="" value="<?php echo $totime;?>"/></td>
<td><input type="text" name="txtAssignedFromDate_
<?php echo $location['pk_locationid'];?>"
class="date-pick field" style="width:80px;"/></td>
<td><input type="text" name="txtAssignedToDate_
<?php echo $location['pk_locationid'];?>"
class="date-pick field" style="width:80px;"/></td>
<td><input type="text" name="txtAssignedFromTime_
<?php echo $location['pk_locationid'];?>"
class="time-pick field" style="width:80px;" /></td>
<td><input type="text" name="txtAssignedToTime_
<?php echo $location['pk_locationid'];?>"
class="time-pick field" style="width:80px;"/></td></tr>
<?php
}
?>
</table>
理想情况下,应为班加罗尔和chennai位置显示数据,因为表tbl_collectiondesireddatetimelocations包含这些位置的数据。但是我得到了错误的结果:其他地方也重复了数据
输出错误:
我很困惑我错了?
答案 0 :(得分:1)
问题是,变量$ fromdate,$ todate,aso。即使找不到位置,仍然会在第二次运行中设置。
作为快速修复,您可以在内循环之前重置它们:
foreach($locations as $location)
{
$fromdate='';
$todate='';
$fromtime='';
$totime='';
foreach($desireddatetimelocations as $desireddatetimelocation)
{
另一种方法是创建location =&gt;日期/时间映射的关联数组,并在循环中使用它:
<?php
$dateTimes = array();
foreach($desireddatetimelocations as $desireddatetimelocation)
{
$dateTimes[$desireddatetimelocation['fk_tbl_locations_locationid']] =
$desireddatetimelocation;
}
foreach($locations as $location)
{
?>
<tr>
<td><?php echo $location['name'];?></td>
<td>
<input type="text" name="txtFromDate_<?php echo $location['pk_locationid'];?>"
class="field" style="width:80px;" readonly=""
value="<?php echo isset($dateTimes[$location['pk_locationid']]) ?
$dateTimes[$location['pk_locationid']]['fromdate'] : '';?>"/>
</td>
.....