【问题标题】:Problems displaying array results in table using CodeIgniter使用 CodeIgniter 在表格中显示数组结果时出现问题
【发布时间】:2014-09-02 18:48:14
【问题描述】:

我需要一些帮助来显示 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();
}

【问题讨论】:

    标签: php mysql arrays codeigniter


    【解决方案1】:

    以下 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 子句,这可能有助于清晰和健全,这样您就可以确保您的结果正确相关。

    【讨论】:

    • 感谢乔尔的回复。我同意,这就是正在发生的事情。在我的控制器中,教育者 ID 被正确传递。在我看来,我将不得不努力纠正这一点。另外,感谢关​​于 order_by 条款的建议。好主意。
    猜你喜欢
    • 2019-07-19
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2019-11-03
    • 2011-07-04
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多