首页 > 解决方案 > 比较上一行的值

问题描述

我有一个工作和口译员数据库。我正在尝试为每位口译员制作一张表格,以查看他们下周的工作,但我无法对它们进行排序。现在它一直将每个作业放入不同的表中,但我想要的是它为按 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 "&nbsp";
        echo "ASSIGNED";
        echo "</font>";
        echo "</b>";
        echo "</td>";


    }    else {
        echo "<td>";
        echo $row['First_Name'];
        echo "&nbsp";
        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>";





标签: php

解决方案


我支持@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 "&nbsp";
      echo "ASSIGNED";
      echo "</font>";
      echo "</b>";
      echo "</td>";
  }   else {
      echo "<td>";
      echo $row['First_Name'];
      echo "&nbsp";
      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>";

在此处查看演示:https ://rextester.com/WNAOTN55281


推荐阅读