php - 比较上一行的值
问题描述
我有一个工作和口译员数据库。我正在尝试为每位口译员制作一张表格,以查看他们下周的工作,但我无法对它们进行排序。现在它一直将每个作业放入不同的表中,但我想要的是它为按 first_name 排序的每个解释器将其保存在同一个表中,然后结束该表并为下一个解释器创建一个新表。我想也许我可以制作某种变量来存储前一行的值,然后以echo "</table>";
但我还没弄清楚怎么做。我想如果我最终可以让他们分组,我最终可以将 html 表格放入电子邮件中。sql 是一个保存的视图,它已经按 first_name 排序,但也返回 inter_id 的值(即使该部分未显示在表中)。我在想我可以比较每一行上 inter_id 的值。这就是我现在所拥有的:
$table_start="
<table style=\"width:100%\">
<tr>
<th>Date & Time</th>
<th>Interpreter</th>
<th>Subject</th>
<th>Location</th>
</tr>
";
if ($result = $conn->query($sql)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
$outcome_id=$row['outcome_id'];
$inter_id=$row['inter_id'];
$start = $row['start'];
$beginning = hr_datetime($start); //this function is elsewhere
echo $table_start;
echo"<tr>";
echo "<td>";
echo $beginning;
echo "</td>";
if ($cur_inter_id === NULL){
echo "<td>";
echo "<font color=\"red\">"; //I want it obvious someone is not assigned
echo "<b>"; //bold
echo "NOT";
echo " ";
echo "ASSIGNED";
echo "</font>";
echo "</b>";
echo "</td>";
} else {
echo "<td>";
echo $row['First_Name'];
echo " ";
echo $row['Last_Name'];
echo "</td>";
}
echo "<td>";
if ($outcome_id === '9'){echo "<strike> <font color=\"orange\"> <b>";} //I want it to be obvious an app is cancelled,
echo $row['subject'];
if ($outcome_id === '9'){echo "/CANCELLED </strike></font></b>";}
echo "</td>";
echo "<td>";
echo $row['location'];
echo "</td>";
}
}
echo "</table>";
解决方案
我支持@RamRaider 写的所有内容。特别是关于您格式化表格的方式。
但是,我在您的原始脚本的基础上准备了一个小小提琴来演示如何为每个解释器创建单独的表:
$prev_id="start";
echo $table_start;
foreach ($dat as $row){ // (this emulates the database)
$outcome_id=$row['outcome_id'];
$inter_id=$row['inter_id'];
$cur_inter_id=$inter_id;
$start = $row['start'];
$beginning = $start; // hr_datetime($start); //this function is elsewhere
// ============================================================================
// compare the current ID with the previous one and create table division here:
if ($prev_id!="start" && $inter_id!=$prev_id) echo "</table>\n\n".$table_start;
// ============================================================================
echo"\n<tr>";
echo "<td>";
echo $beginning;
echo "</td>";
if ($cur_inter_id === NULL){
echo "<td>";
echo "<font color=\"red\">"; //I want it obvious someone is not assigned
echo "<b>"; //bold
echo "NOT";
echo " ";
echo "ASSIGNED";
echo "</font>";
echo "</b>";
echo "</td>";
} else {
echo "<td>";
echo $row['First_Name'];
echo " ";
echo $row['Last_Name'];
echo "</td>";
}
echo "<td>";
if ($outcome_id == '9'){echo "<strike><font color=\"orange\"> <b>";} //I want it to be obvious an app is cancelled,
echo $row['subject'];
if ($outcome_id == '9'){echo "/CANCELLED </font></strike></b>";}
echo "</td>";
echo "<td>";
echo $row['location'];
echo "</td>";
echo "</tr>";
$prev_id=$inter_id;
}
echo "</table>";
推荐阅读
- node.js - EC2、Node.js - 调用位于同一服务器上的 API
- r - 在 R 中的文本文件中存储巨大矩阵的好方法
- git - core.eol = crlf 在添加时而不是在结帐时设置 crlfs
- r - geom_segment 上自定义调色板中的随机颜色
- r - 带有刻面网格的哑铃图错误,无法创建并排图
- python - 旋转边界框的联合可微损失的交集
- django - 如何在 django 中更改文件的路径
- python - ursina 引擎错误但仍在运行,为什么?
- embedded - 动态传感器校准
- java - 无法在 AndroidStudio 中实例化活动 ComponentInfo