比较上一行的值

时间:2019-10-27 07:26:49

标签: php

我有一个工作和口译员数据库。我正在尝试为每个口译员制作一张桌子,以查看他们下一周的工作,但是我在排序时遇到了麻烦。现在,它会将每个作业放到不同的表中,但我想要的是将它按first_name的顺序将每个解释器保留在同一表中,然后结束该表并为下一个解释器创建一个NEW表。我以为也许可以做一些变量来存储上一行的值,然后以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>";





2 个答案:

答案 0 :(得分:0)

请参阅上面的注释,以解释为什么结果出现在不同的表中-这只是整理好的版本,至少应正确呈现该表。 css应该在其他地方定义-如果可以,请不要使用内联样式定义。

if( $result = $conn->query( $sql ) ) {

    echo '
        <style>
            table{width:100%}
            .bold{font-weight:bold}
            .red{color:red}
            .orange{color:orange}
            .green{color:green}
        </style>


        <table>
            <tr>
                <th>Date & Time</th>
                <th>Interpreter</th>
                <th>Subject</th>
                <th>Location</th>
            </tr>';


    while( $row = $result->fetch_assoc() ) {

        $inter_id=$row['inter_id'];
        $start=$row['start'];

        $interpreter = is_null( $cur_inter_id ) ? "<span class='bold red'>NOT ASSIGNED</span>" : sprintf( '%s %s', $row['First_Name'], $row['Last_Name'] );
        $subject=intval( $row['outcome_id'] )==9 ? "<span class='bold orange'>CANCELLED</span>" : "<span class='green'>OK</span>";

        printf('
                <tr>
                    <td>%s</td>
                    <td>%s</td>
                    <td>%s</td>
                    <td>%s</td>
                </tr>',
                hr_datetime( $start ),
                $interpreter,
                $subject,
                $row['location']
        );
    }//end loop

    echo '
        <table>';

}//end if

答案 1 :(得分:0)

我将@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