开发者

How do i fetch data (table) from inner loop dependant upon outer loop?

I have two tables named tbl_collectiondesireddatetimelocations and tbl_locations

Structure of tbl_collectiondesireddatetimelocations is :

pk_collectiondesireddatetimelocationid
, fk_tbl_locations_locationid, fromdate, todate, fromtime, totime 

And structure of tbl_locations is

pk_locationid, name

I have following page:

How do i fetch data (table) from inner loop dependant upon outer loop?

In the first column location is populated from tbl_locations table.

What i want is to fill the data for the requested column? Data will be retrieved from the tbl_collectiondesireddatetimelocations. For e.g. location id of bangalore is 1. I will check if there is a location id 1 in the tbl_collectiondesireddatetimelocations fk_tbl_locations_locationid column. If it exists it will retrieve the fromdate,todate,fromtime,totime column values and fill that data for that particular location.

I have written following code :

<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" na开发者_Go百科me="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>

Ideally data should be displayed for bangalore and chennai location as table tbl_collectiondesireddatetimelocations contains data for these locations. But i am getting wrong result:Data is repeated for other locations also

Wrong output:

How do i fetch data (table) from inner loop dependant upon outer loop?

I am confused where I am getting this wrong?


The problem is, that the variables $fromdate, $todate, aso. are still set in the second run even if no location found.

As a quick fix you can just reset them before the inner loop:

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

  foreach($desireddatetimelocations as $desireddatetimelocation)
  {

Another approach would be to create an associative array of the location=>date/time mapping and use that in the loop:

<?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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜