我有兩個表名爲tbl_collectiondesireddatetimelocations和tbl_locations我如何從內循環取決於外循環的數據(表)?
的tbl_collectiondesireddatetimelocations結構是:
pk_collectiondesireddatetimelocationid
, fk_tbl_locations_locationid, fromdate, todate, fromtime, totime
和tbl_locations結構
pk_locationid, name
我有以下頁面:
第一列中的位置是從tbl_locations表中填充的。
我想要的是填寫所需列的數據?數據將從tbl_collectiondesireddatelocations中檢索。對於例如班加羅爾的位置ID爲1.我將檢查tbl_collectiondesireddatelocations 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>
理想的數據應該顯示班加羅爾和欽奈的位置如表tbl_collectiondesireddatetimelocations包含這些位置數據。但是我得到錯誤的結果:重複其他地方的數據也
錯誤輸出:
我很困惑在那裏我得到這個錯了嗎?
謝謝chrifin回覆。我根據您的建議進行了更改,並且工作正常。再次感謝您的寶貴支持。 –