带有rowspan的动态表

时间:2017-04-14 04:28:34

标签: php

我有一个表生成一个循环,其中列标题通过fri和时间数组以30分钟为间隔24小时我刚刚缩短了示例代码:

$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>

现在我的代码可以很好地生成带有rowspan的表,但我知道我必须在下一行中删除必要的td,这取决于我的rowspan($ res)包含多少行。这可能是我从公共汽车上摔下来的部分:) 我在这里想要实现的是获取行号和“td”。隐藏在

后面的行中
// 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";
 }
}

我甚至不确定我是否在正确的问题上接近整个概念,我们将非常感谢任何帮助。 谢谢!!

在一天结束时,我的表应如下所示:

table

1 个答案:

答案 0 :(得分:0)

这是您的填写答案

好吧,我没有时间来制作一个完整的阵列,但希望它对你有用。

<?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>