gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 07:53:58 25 4
gpt4 key购买 nike

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

下面的代码正确输出了一个包含 8 周的表格(按行),并且还显示了我可用的教育者(按列)。我的日期一次正确增加 1 周,但我遇到的问题是根据 Educator_Id 显示正确的可用性。我的 View 似乎没有正确地循环浏览教育者,因为每个教育者都显示相同的状态。查看 mysql 日志,显示执行了正确的查询(我可以看到传递了多个 id)。

这是我的 table 的样子。第 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>

我的 Controller :

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 和 $educator_availability 数组。相反,您可能会访问每个数组中的相同元素。

换句话说,考虑两个数组。您可以这样循环一个数组:

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

您在该循环中要做的就是打印出 $foos 的每个元素的 $bar 值。

相反,您需要根据 for 循环的特定迭代中引用的教育者来访问 $week、$master、$educator_schedule 和 $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 子句,这样您就可以确保您的结果会更清晰、更清晰。正确相关。

关于php - 使用 CodeIgniter 在表中显示数组结果时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25630245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com