簡體   English   中英

使用CodeIgniter在表中顯示數組結果時出現問題

[英]Problems displaying array results in table using CodeIgniter

我需要一些幫助來顯示html表中數組的正確值。 我正在編寫一種調度引擎,該引擎將允許用戶根據首選的日期/時間來調度課程,並檢查教育工作者的可用性。

下面的代碼正確地輸出了一個8周的表格(行),並且還顯示了我可用的教育者(列)。 我的日期每次正確地增加1周,但是我遇到的問題是根據Educator_Id顯示正確的可用性。 我的觀點似乎無法正確地遍歷所有教育者,因為每個教育者都顯示相同的狀態 查看mysql日志,顯示已執行正確的查詢(我可以看到已傳遞多個ID)。

這是我的桌子的樣子。 第4周應該只顯示Jane Doe的個人經歷。

謝謝你提供的所有幫助。

這是我的桌子

我的觀點:

<table>
 <thead>
  <tr>
   <th>Week #</th>
   <th>Date</th>
   <th>Time</th>
   <?php foreach($educators as $educator): ?>
    <th><?php echo $educator->First_Name.' '.$educator->Last_Name; ?></th>
   <?php endforeach; ?>
  </tr>
 </thead>

 <tbody>

 <?php
 $i = 0;
 foreach($weeks as $key => $week):
 $class_date= $class_dates[$key];
 $week = $weeks[$key];
 $master_schedule = $master_schedules[$key];
 $educator_schedule = $educator_schedules[$key];
 $educator_availability = $educator_availabilities[$key];
 $week_day_id = $i + 1;
 ?>
  <tr>
   <td>Week <?php echo $week_day_id; ?></td>
   <td><?php echo $class_date; ?></td>
   <td><?php echo $opportunity->First_Preferred_Start_Time.' - '.$opportunity->First_Preferred_End_Time; ?></td>

   <?php foreach($educators as $educator): ?>
   <td>
    <?php if($week->Count == 1): echo 'Class Conflict'; endif; ?>
    <?php if($master_schedule->Count == 1): echo $master_schedule->Title; endif; ?>
    <?php if($educator_schedule->Count == 1): echo $educator_schedule->Title; endif; ?>
    <?php if($educator_availability->Count == 1): echo 'Not Scheduled'; endif; ?>
   </td>
   <?php endforeach; ?>
  </tr>
  <?php if($i ++ == 7) break; endforeach; ?>
 </tbody>
</table>

我的控制器:

function schedule_opportunity($Opportunity_Id) {
    //retrieve opportunity details
    $this->data['opportunity'] = $this->ion_auth_model->get_opportunity($Opportunity_Id)->row();
    $opportunity = $this->ion_auth_model->get_opportunity($Opportunity_Id)->row();

    $Curricula_Id = $opportunity->Curricula_Id;
    $Preferred_Start_Time = $opportunity->First_Preferred_Start_Time;
    $Preferred_End_Time = $opportunity->First_Preferred_End_Time;
    $Preferred_Start_Date = $opportunity->First_Preferred_Start_Date;

    //find available educators
    $this->data['educators'] = $this->ion_auth_model->available_educators($Curricula_Id);
    $educators = $this->ion_auth_model->available_educators($Curricula_Id);

    foreach($educators as $educator):
        $Educator_Id = $educator->Educator_Id;

        for($i=0; $i < 8; $i++):
            $Date = strtotime("+$i week", strtotime($Preferred_Start_Date));
            $Class_Date = date("Y-m-d", $Date);

            $this->data['class_dates'][] = date("Y-m-d", $Date);
            $this->data['weeks'][] = $this->ion_auth_model->week($Educator_Id, $Class_Date, $Preferred_Start_Time, $Preferred_End_Time)->row();
            $this->data['master_schedules'][] = $this->ion_auth_model->master_schedule_check($Class_Date, $Preferred_Start_Time, $Preferred_End_Time)->row();
            $this->data['educator_schedules'][] = $this->ion_auth_model->educator_schedule_check($Educator_Id, $Class_Date, $Preferred_Start_Time, $Preferred_End_Time)->row();
            $this->data['educator_availabilities'][] = $this->ion_auth_model->educator_availability_check($Educator_Id, $Class_Date, $Preferred_Start_Time, $Preferred_End_Time)->row();
        endfor;
    endforeach;

    $this->data['main_content'] = 'schedule_opportunity';
    $this->load->view('./_blocks/template', $this->data);
}

我的模特:

function educator_schedule_check($Educator_Id, $Class_Date, $Preferred_Start_Time, $Preferred_End_Time) {
    $Date = "('$Class_Date' BETWEEN Start_Date AND End_Date AND All_Day = 1 AND Educator_Id = '$Educator_Id' OR '$Class_Date' BETWEEN Start_Date AND End_Date AND (Start_Time BETWEEN '$Preferred_Start_Time' AND '$Preferred_End_Time' OR End_Time BETWEEN '$Preferred_Start_Time' AND '$Preferred_End_Time'))";

    $this->db->select("Count(*) AS Count, Title")->from('Educators_Schedule')->where($Date)->where('Educator_Id', $Educator_Id);
    return $this->db->get();
}

關於以下for循環的某些事情似乎不太正確,盡管如果不使用調試器很難確定。

<?php foreach($educators as $educator):
  <td>
    <?php if($week->Count == 1): echo 'Class Conflict'; endif; ?>
    <?php if($master_schedule->Count == 1): echo $master_schedule->Title; endif; ?>
    <?php if($educator_schedule->Count == 1): echo $educator_schedule->Title; endif; ?>
    <?php if($educator_availability->Count == 1): echo 'Not Scheduled'; endif; ?>
  </td>
<?php endforeach; ?>

我看到您正在遍歷教育者,但是我看不到當前的教育者實際上在迭代過程中發生了變化。 您似乎並沒有遍歷$week, $master, $educator_schedule, and $educator_availability數組。 相反,您可能正在每個數組中訪問相同的元素。

換句話說,考慮兩個數組。 您這樣循環遍歷一個數組:

 <?php
     foreach($foos as $foo):
         echo $bar;
     endforeach;
 ?>

您在該循環中要做的就是為$foos每個元素打印$bar的值。

相反,您需要根據在for循環的特定迭代中引用的教育者來訪問$week, $master, $educator_schedule, and $educator_availability的元素,以便使這些功能起作用:

<?php foreach($educators as $educator):
  <td>
    <?php if($week[$educator]->Count == 1): echo 'Class Conflict'; endif; ?>
    <?php if($master_schedule[$educator]->Count == 1): echo $master_schedule->Title; endif; ?>
    <?php if($educator_schedule[$educator]->Count == 1): echo $educator_schedule->Title; endif; ?>
    <?php if($educator_availability[$educator]->Count == 1): echo 'Not Scheduled'; endif; ?>
  </td>
<?php endforeach; ?>

當前狀態下的代碼無法正常工作,但可能會為您指明正確的方向。

最后,我有點擔心您如何控制從數據庫查詢返回的元素的順序。 在大多數情況下,您的結果將以相同的順序返回,但是如果您向某些查詢中添加order_by子句,則可以使結果清晰明了,從而確保結果正確相關。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM