使用CodeIgniter在表中显示数组结果时出现问题

时间:2014-09-02 18:48:15

标签: php mysql arrays codeigniter

我需要一些帮助来显示html表中数组的正确值。我正在编写各种调度引擎,允许用户根据首选日期/时间安排课程,并检查教育工作者的可用性。

下面的代码正确输出一个8周(行数)的表格,并显示我可用的教育工作者(列中)。我的日期一次正确增加1周,但我遇到的问题是显示基于Educator_Id的正确可用性。我的观点似乎没有正确地贯穿教育者,因为每个教育者都会显示相同的状态。查看mysql日志,表明执行了正确的查询(我可以看到已经传递了多个id)。

这是我的桌子的样子。第4周应仅为Jane Doe显示个人日

感谢您提供任何帮助。

Here's my table

我的观点:

<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();
}

1 个答案:

答案 0 :(得分:2)

关于以下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;
 ?>

您在该循环中所做的一切就是为$bar的每个元素打印$foos的值。

相反,您需要根据在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子句,则可能有助于清晰和完整,这样您就可以确保结果正确相关