dongwo2222 2017-04-14 04:28
浏览 137
已采纳

带有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 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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算