Search code examples
phpcsscolorsjscriptgroup

php table colour rows group by date


New to stackoverflow. About 5 years ago I built a booking site and haven't touch php since, so very very rusty. 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. This table can now have 1, 2, 3 or 4 results for the same date. 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:

enter image description here

and this is what I would like:

enter image description here


Solution

  • 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).

    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>";    
    }