dongwo2222
2017-04-14 04:28 阅读 106
已采纳

带有rowspan的动态表

I have a table generated with a loop with column headers mon trough to fri and time array with 30min intervals for 24hr I have just shorten the example code:

$times = array(00:00, 00:30, 01:00, 01:30);
$skip = array();
$row = 1;
<table>
 <tr>
  <td>Time</td><td>Mon</td><td>tue</td>
 <tr>
 foreach($times AS $val){
  $stmt = $mysqli->prepare("
  SELECT `show`,`time_start`,`time_stop`,`show_day` 
  FROM `db_shows` WHERE `time_start`='$val'
  ");
  $stmt->execute();
  $stmt->store_result();
  $stmt->bind_result($myrow[show],$myrow[time_start],$myrow[time_stop],$myrow[show_day]);
  $stmt->fetch();
  // GET THE ROWSPAN
  $a = new DateTime($myrow[time_start]);
  $b = new DateTime($myrow[time_stop]);
  $interval = $a->diff($b);
  $res = $interval->format('%h');
  $res *= 2;
  $res += 1;
  // ARRY FOR ROWS TO SKIP TD
  code in second part
  // APPLAY DISPLAY NONE IF NEEDED
  code in second part
  // BUILD COLUMNS
  switch($myrow[show_day]){
   case "":$tds = "<td>&nbsp;</td><td>&nbsp;</td>"; break;
   case "mon":$tds = "<td rowspan="$res" style="$mon">$myrow[show]</td><td>&nbsp;</td>"; break;
   case "tue":$tds = "<td>&nbsp;</td><td rowspan="$res" style="$tue">$myrow[show]</td>"; break;
  }
  <tr>
   <td>$val</td>$tds
  </tr>
 $row++;
 }     
</table>

Now my code works fine generating the the table with the rowspan, but I know I have to remove the necessary td's in the next rows depending on how many rows my rowspan ($res) contain. This is probably the part I'm falling out of the bus :) What I'm trying to achieve here is to get the row number and 'day td' to hide in the rows that follow

// ARRY FOR ROWS TO SKIP TD
if($res >=2){
$rowCount = $res -1;
$rowSkip = $row;
for($i=1;$i <= $rowCount; $i++){
$rowSkip++;
$skip[$rowSkip] = $myrow[show_day];
}
}
// LOOK IF ROW HAVE VALUE
foreach($skip AS $key2 => $val2){
 if($row == "$key2" && $myrow[show_day]="$val2"){
  $$val2 = "display:none";
 }
}

I'm not even sure if I'm approaching the whole concept on the right matter, any help will be greatly appreciated. Thank You!!

In the end of the day my table should look like this:

table

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    doukan6564 doukan6564 2017-04-14 05:53

    Here is your fill answer

    Well i don't have good time to cake an full array but hopefully it will work for you.

    <?php
    $times = array('00:00','00:30', '01:00', '01:30','02:00','02:30','03:00');
    $days = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'); // 0 = Sun ... 6 = Sat
    
    
    $shows = array();
    
    $shows[] =   array(
        "day"=>0,
        "time_start"=>'00:00',
        "time_end"=>'00:30',
        "name"=>"Show A"
        );
    
    $shows[] = array(
        "day"=>1,
        "time_start"=>'00:00',
        "time_end"=>'00:30',
        "name"=>"Show B"
        );
    
    $shows[] = array(
        "day"=>3,
        "time_start"=>'00:00',
        "time_end"=>'00:30',
        "name"=>"Show C"
        );
    
    $shows[] = array(
        "day"=>4,
        "time_start"=>'00:00',
        "time_end"=>'00:30',
        "name"=>"Show D"
        );
    
    $shows[] = array(
        "day"=>5,
        "time_start"=>'00:00',
        "time_end"=>'00:30',
        "name"=>"Show E"
        );
    
    $shows[] = array(
        "day"=>6,
        "time_start"=>'00:00',
        "time_end"=>'00:30',
        "name"=>"Show F"
        );
    
    $shows[] = array(
        "day"=>1,
        "time_start"=>'00:30',
        "time_end"=>'01:30',
        "name"=>"Show G"
        );
    
    $shows[] = array(
        "day"=>2,
        "time_start"=>'00:30',
        "time_end"=>'01:00',
        "name"=>"Show H"
        );
    $shows[] = array(
        "day"=>3,
        "time_start"=>'00:30',
        "time_end"=>'01:00',
        "name"=>"Show I"
        );
    $shows[] = array(
        "day"=>4,
        "time_start"=>'00:30',
        "time_end"=>'01:00',
        "name"=>"Show J"
        );
    $shows[] = array(
        "day"=>1,
        "time_start"=>'01:00',
        "time_end"=>'01:30',
        "name"=>"Show K"
        );
    
    $shows[] = array(
        "day"=>2,
        "time_start"=>'01:00',
        "time_end"=>'02:00',
        "name"=>"Show L"
        );
    
    $shows[] = array(
        "day"=>1,
        "time_start"=>'01:30',
        "time_end"=>'02:00',
        "name"=>"Show M"
        );
    
    $shows[] = array(
        "day"=>2,
        "time_start"=>'01:30',
        "time_end"=>'02:00',
        "name"=>"Show N"
        );
    
    $parsedShow = array();
    
    foreach ($shows as  $show) {
    
        $start_index =  array_search($show['time_start'], $times); // $key = 2;
        $end_index =  array_search($show['time_end'], $times); // $key = 1;
    
        if($end_index - $start_index > 1){
            //NEED SPAN
            $show['span'] = (($end_index - $start_index));
        }else{
            $show['span'] = false;
        }
        $parsedShow[$show['time_start']][] = $show;
    }
    
    // echo "<pre>";
    // print_r($parsedShow);
    
    // die();
    ?>
    <html>
    <table border="1">
        <tr>
            <td>
                Time
            </td>
            <?php 
            foreach ($days as $day) {
                echo "<td>$day</td>";
            }
            ?>
    
        </tr>
        <?php 
        foreach ($times as $time) {
            ?>
            <tr>
                <?php 
                if(!isset($parsedShow[$time])){
                    continue;
                }
                echo "<td>$time</td>";
                foreach ($parsedShow[$time] as $show) {
                    ?>
                    <td <?= ($show['span'] !== false ? "rowSpan='".$show['span']."'" : "")?>><?= $show['name'] ?></td>
                    <?php }
                    ?>
                </tr>
                <?php }
    
                ?>
            </table>
            </html>
    
    点赞 评论 复制链接分享

相关推荐