2011-08-30 23 views
0

我有兩個表名爲tbl_collectiondesireddatetimelocationstbl_locations我如何從內循環取決於外循環的數據(表)?

tbl_collectiondesireddatetimelocations結構是:

pk_collectiondesireddatetimelocationid 
, fk_tbl_locations_locationid, fromdate, todate, fromtime, totime 

tbl_locations結構

pk_locationid, name 

我有以下頁面:enter image description here

第一列中的位置是從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包含這些位置數據。但是我得到錯誤的結果:重複其他地方的數據也

錯誤輸出:

enter image description here

我很困惑在那裏我得到這個錯了嗎?

回答

1

問題是,變量$ fromdate,$ todate,aso。即使沒有找到位置,仍然會在第二次運行中設置。

作爲一個快速修復你只是內部循環之前重置它們:

foreach($locations as $location) 
{ 
    $fromdate=''; 
    $todate=''; 
    $fromtime=''; 
    $totime=''; 

    foreach($desireddatetimelocations as $desireddatetimelocation) 
    { 

另一種方法是創建位置=>日期/時間映射的關聯數組和使用,在循環:

<?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> 
..... 
+0

謝謝chrifin回覆。我根據您的建議進行了更改,並且工作正常。再次感謝您的寶貴支持。 –