简体   繁体   English

php 表格颜色行按日期分组

[英]php table colour rows group by date

New to stackoverflow. stackoverflow 的新手。 About 5 years ago I built a booking site and haven't touch php since, so very very rusty.大约 5 年前,我建立了一个预订网站,从那以后就再也没有接触过 php,所以非常生疏。 All is working well with it.一切都很好。 However I want to make some updates.但是我想做一些更新。 As part of the update I have a table that is grouped by date from a Mysql table.作为更新的一部分,我有一个表,该表按 Mysql 表中的日期分组。 This table can now have 1, 2, 3 or 4 results for the same date.此表现在可以在同一日期有 1、2、3 或 4 个结果。 To make things clearer for the user I would like to colour the rows by date.为了让用户更清楚,我想按日期为行着色。 It can be grey for one date, white for the next, grey for the next as so on.一个日期可以是灰色,下一个日期可以是白色,下一个日期可以是灰色,依此类推。 I do have code to colour all the dates, unfortunately I can end up with consecutive dates with the same colour.我确实有为所有日期着色的代码,不幸的是,我最终可能会得到具有相同颜色的连续日期。

while ($row = mysqli_fetch_assoc($results)) {
    $id_array[] = $row["id"];
    $Fltnum_array[] = $row["fltnum"];
    $Date_array[] = $row["Date"];
    $Name_array[] = $row["Name"];
    $Lesson_array[] = $row["vouchertype"];
    $Vnum_array[] = $row["vnum"];
    $Phone_array[] = $row["phone"];
    $Email_array[] = $row["email"];
    $Ten_array[] = $row["Ten"];
    $Ten1_array[] = $row["Ten1"];
    $Eleven_array[] = $row["Eleven"];
    $Twelve_array[] = $row["Twelve"];
    $Thirteen_array[] = $row["thirteen"];
    $Fourteen_array[] = $row["Fourteen"];
    $Fithteen_array[] = $row["Fifteen"];
    $Sixteen_array[] = $row["Sixteen"]; 



}



<body style="left: 50%; position: absolute; height:1000px; ">
<div id="apDiv3">
<?php
    echo "<form action='#' name='mainForm' method='post'>
       <table border='1' align='center' cellpadding='3' cellspacing='0' >
           <tr>
                <th>Name</th>
                <th>Flight Type</th>
                <th>Date</th>
                <th>10:00</th>
                <th>10:00</th>
                <th>11:00</th>
                <th>12:00</th>
                <th>13:00</th>
                <th>14:00</th>
                <th>15:00</th>
                <th>16:00</th> 
             </tr>";
    for ($i = 0; $i < count($id_array); $i++) {
        $id = $id_array[$i];
        $Fltnum = $Fltnum_array[$i];
        $Date = $Date_array[$i];
        $Name = $Name_array[$i];
        $Lesson = $Lesson_array[$i];
        $Vnum = $Vnum_array[$i];
        $Phone = $Phone_array[$i];
        $Email = $Email_array[$i];
        $Ten = $Ten_array[$i];
        $Ten1 = $Ten1_array[$i];
        $Eleven = $Eleven_array[$i];
        $Twelve = $Twelve_array[$i];
        $Thirteen = $Thirteen_array[$i];
        $Fourteen = $Fourteen_array[$i];
        $Fithteen = $Fithteen_array[$i];
        $Sixteen = $Sixteen_array[$i];
        $set_checked = ($Ten < 1) ? 'checked="checked"' : '';
        $set_checked0 = ($Ten1 < 1) ? 'checked="checked"' : '';
        $set_checked1 = ($Eleven < 1) ? 'checked="checked"' : '';
        $set_checked2 = ($Twelve < 1) ? 'checked="checked"' : '';
        $set_checked3 = ($Thirteen < 1) ? 'checked="checked"' : '';
        $set_checked4 = ($Fourteen < 1) ? 'checked="checked"' : '';
        $set_checked5 = ($Fithteen < 1) ? 'checked="checked"' : '';
        $set_checked6 = ($Sixteen < 1) ? 'checked="checked"' : '';
        $newDate = date("d-m-Y", strtotime($Date));         
    echo "
        <input type='hidden' name='id[]' value='$id'>
        <input type='hidden' name='Fltnum-$id' value='$Fltnum'> 
        <td><input input type='text' name='Name-$id' value='$Name'></td>
        <td><input input type='text' name='Lesson-$id' value='$Lesson'></td>
        <td><input type='text' name='Date-$id' value='$newDate'> </td>
        <input type='hidden' name='vnum-$id' value='$Vnum'>
        <input type='hidden' name='Phone-$id' value='$Phone'>
        <input type='hidden' name='Email-$id' value='$Email'>";
        ...
        echo                
        "</tr>";    
        }
        echo "<tr align='center'><td colspan='8'><input type='submit' name='submit' value='Make Booking' ></td></tr></table>";  
    echo "</form>";
 ?>
</div>

I have spent a couple of days on this also looking through the forums.我也花了几天时间浏览论坛。

To Add a little more detail, This is the result as is:要添加更多细节,这是结果:

在此处输入图像描述

and this is what I would like:这就是我想要的:

在此处输入图像描述

It appears as though you want to alternate colors for the rows, but only if the date is different than the previous row (so consecutive dates that are the same will appear the same color).看起来好像您想为行交替显示 colors,但前提是日期与前一行不同(因此相同的连续日期将显示相同的颜色)。

You could do something like this by comparing the date in your loop with the previous date and then applying a color from an array based on whether or not they are the same.您可以通过将循环中的日期与前一个日期进行比较,然后根据它们是否相同来应用数组中的颜色来执行类似的操作。

$backgrounds = ["#FFFFFF", "#EEEEEE"];
$currentBackground = 0;

for ($i = 0; $i < count($id_array); $i++) {
  $id = $id_array[$i];
  $Fltnum = $Fltnum_array[$i];
  $Date = $Date_array[$i];
  $Name = $Name_array[$i];
  $Lesson = $Lesson_array[$i];
  $Vnum = $Vnum_array[$i];
  $Phone = $Phone_array[$i];
  $Email = $Email_array[$i];
  $Ten = $Ten_array[$i];
  $Ten1 = $Ten1_array[$i];
  $Eleven = $Eleven_array[$i];
  $Twelve = $Twelve_array[$i];
  $Thirteen = $Thirteen_array[$i];
  $Fourteen = $Fourteen_array[$i];
  $Fithteen = $Fithteen_array[$i];
  $Sixteen = $Sixteen_array[$i];
  $set_checked = ($Ten < 1) ? 'checked="checked"' : '';
  $set_checked0 = ($Ten1 < 1) ? 'checked="checked"' : '';
  $set_checked1 = ($Eleven < 1) ? 'checked="checked"' : '';
  $set_checked2 = ($Twelve < 1) ? 'checked="checked"' : '';
  $set_checked3 = ($Thirteen < 1) ? 'checked="checked"' : '';
  $set_checked4 = ($Fourteen < 1) ? 'checked="checked"' : '';
  $set_checked5 = ($Fithteen < 1) ? 'checked="checked"' : '';
  $set_checked6 = ($Sixteen < 1) ? 'checked="checked"' : '';
  $newDate = date("d-m-Y", strtotime($Date));

  // Only check previous value if we are not on the first row
  if($i > 0) {
    // If the date is the same, do not change backgrounds
    // Otherwise, toggle between backgrounds
    $currentBackground = ($Date_array[$i] == $Date_array[$i-1]) ? $currentBackground : ($currentBackground == 0) ? 1 : 0;
  }
  // This just sets the color from our backgrounds array
  $background = $backgrounds[$currentBackground];
  
  echo "
    <tr style='background: $background;'>
      <td><input type='hidden' name='id[]' value='$id'></td>
      <td><input type='hidden' name='Fltnum-$id' value='$Fltnum'></td>
      <td><input input type='text' name='Name-$id' value='$Name'></td>
      <td><input input type='text' name='Lesson-$id' value='$Lesson'></td>
      <td><input type='text' name='Date-$id' value='$newDate'> </td>
      <td><input type='hidden' name='vnum-$id' value='$Vnum'></td>
      <td><input type='hidden' name='Phone-$id' value='$Phone'></td>
      <td><input type='hidden' name='Email-$id' value='$Email'></td>
    </tr>";    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM