Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中使用rowspan并将数组输出到表_Php_Arrays_Html Table - Fatal编程技术网

在php中使用rowspan并将数组输出到表

在php中使用rowspan并将数组输出到表,php,arrays,html-table,Php,Arrays,Html Table,我希望修改页面上的表以包含合并行 以下是处理mysql数据库输出的php代码: ######## PRINT OUT TABLE WITH YEARS AND OFFICES PLUS NAMES IN CELLS ######## for ($y = $year_max; $y >=$year_min; $y--){ echo '<tr><th>'.$y.'</th>'; for ($i = 0; $i<count($o

我希望修改页面上的表以包含合并行

以下是处理mysql数据库输出的php代码:

######## PRINT OUT TABLE WITH YEARS AND OFFICES PLUS NAMES IN CELLS ########    
for ($y = $year_max; $y >=$year_min; $y--){
    echo '<tr><th>'.$y.'</th>';

    for ($i = 0; $i<count($offices_used); $i++){

        if (isset($data[$y][$offices_used[$i]])){
            echo '<td>'.$data[$y][$offices_used[$i]].'</td>';
        } // END IF

        else {
            echo '<td></td>';
        } // END ELSE

        }   echo '</tr>';
    } // END FOR 
…等等

我的php代码现在从mysql数据库生成此表:

<tr>
    <th>Year</th>
    <th>President</th>
    <th>Internal VP</th>
    <th>External VP</th>
    <th>Treasurer</th>
    <th>Secretary</th>
    <th>Webmaster</th>
    <th>Newsletter</th>
    <th>Photographer</th>
    <th>Video Librarian</th>
    <th>Book Librarian</th>
    <th>Store Manager</th>
</tr>

<tr>
    <th>2013</th>
    <td>John Mills</td>
    <td>Virgil Bagdonas</td>
    <td>Reid Gilmore</td>
    <td>Todd Heino</td>
    <td>Eric Holmquist</td>
    <td>Dave Eaton</td>
    <td>Art Bodwell</td>
    <td>Rick Angus</td>
    <td>Mike Peters</td>
    <td></td>
    <td>Kevin Nee</td>
</tr>
<tr>
    <th>2012</th>
    <td>Dave Eaton</td>
    <td>Jim Metcalf</td>
    <td>Reid Gilmore</td>
    <td>Mike Peters</td>
    <td>Eric Holmquist</td>
    <td>Dave Eaton</td>
    <td>Art Bodwell</td>
    <td>Peter Wilcox</td>
    <td>Ray Asselin</td>
    <td></td>
    <td>Joe Giroux</td>
</tr>
<tr>
    <th>2011</th>
    <td>Charlie Croteau</td>
    <td>Reid Gilmore</td>
    <td>Rick Angus</td>
    <td>Mike Peters</td>
    <td>Eric Holmquist</td>
    <td>Dave Eaton</td>
    <td>Ron Rocheleau</td>
    <td>Peter Wilcox</td>
    <td>Ray Asselin</td>
    <td>Roger Boisvert</td>
    <td>Mike Smith</td>
</tr>

年
主席:
内部副总裁
外部副总裁
司库
秘书
网站管理员
新闻稿
摄影师
录像馆员
图书管理员
商店经理
2013
约翰·米尔斯
维吉尔·巴格多纳斯
里德·吉尔摩
托德·海诺
埃里克·霍姆奎斯特
戴夫·伊顿
阿特·博德威尔
里克·安格斯
迈克·彼得斯
倪凯文
2012
戴夫·伊顿
吉姆·梅特卡夫
里德·吉尔摩
迈克·彼得斯
埃里克·霍姆奎斯特
戴夫·伊顿
阿特·博德威尔
彼得·威尔科克斯
雷·阿瑟林
乔·吉鲁
2011
查理·克罗托
里德·吉尔摩
里克·安格斯
迈克·彼得斯
埃里克·霍姆奎斯特
戴夫·伊顿
罗恩·罗切洛
彼得·威尔科克斯
雷·阿瑟林
罗杰·博伊斯维特
迈克·史密斯
但我想修改数组处理输出以获得以下内容:

<tr>
    <th>Year</th>
    <th>President</th>
    <th>Internal VP</th>
    <th>External VP</th>
    <th>Treasurer</th>
    <th>Secretary</th>
    <th>Webmaster</th>
    <th>Newsletter</th>
    <th>Photographer</th>
    <th>Video Librarian</th>
    <th>Book Librarian</th>
    <th>Store Manager</th>
</tr>
<tr>
    <th>2013</th>
    <td>John Mills</td>
    <td>Virgil Bagdonas</td>
    <td rowspan= "3">Reid Gilmore</td>
    <td>Todd Heino</td>
    <td rowspan="3">Eric Holmquist</td>
    <td rowspan="9">Dave Eaton</td>
    <td rowspan="2">Art Bodwell</td>
    <td>Rick Angus</td>
    <td>Mike Peters</td>
    <td></td>
    <td>Kevin Nee</td>
</tr>
<tr>
    <th>2012</th>
    <td>Dave Eaton</td>
    <td>Jim Metcalf</td>
    <td rowspan="2">Mike Peters</td>
    <td>Peter Wilcox</td>
    <td rowspan="3">Ray Asselin</td>
    <td></td>
    <td>Joe Giroux</td>
</tr>
<tr>
    <th>2011</th>
    <td>Charlie Croteau</td>
    <td>Rick Angus</td>
    <td>Ron Rocheleau</td>
    <td>Peter Wilcox</td>
    <td>Roger Boisvert</td>
    <td>Mike Smith</td>
</tr>

年
主席:
内部副总裁
外部副总裁
司库
秘书
网站管理员
新闻稿
摄影师
录像馆员
图书管理员
商店经理
2013
约翰·米尔斯
维吉尔·巴格多纳斯
里德·吉尔摩
托德·海诺
埃里克·霍姆奎斯特
戴夫·伊顿
阿特·博德威尔
里克·安格斯
迈克·彼得斯
倪凯文
2012
戴夫·伊顿
吉姆·梅特卡夫
迈克·彼得斯
彼得·威尔科克斯
雷·阿瑟林
乔·吉鲁
2011
查理·克罗托
里克·安格斯
罗恩·罗切洛
彼得·威尔科克斯
罗杰·博伊斯维特
迈克·史密斯
欢迎任何关于如何添加标志或计数器以实现此目的的线索。谢谢

试试这个希望吧。(因为我还没有运行它,但我认为它应该会起作用。)

colspan的代码:

for ($y = $year_max; $y >=$year_min; $y--){
    echo '<tr><th>'.$y.'</th>';

    $lastVal='';
    $buf=array();
    for ($i = 0; $i<count($offices_used); $i++){
        (!isset($data[$y][$offices_used[$i]])?($data[$y][$offices_used[$i]]=''):'');// Im not sure that is really needed

        if($lastVal==$data[$y][$offices_used[$i]]){
            $buf[count($buf)-1]['rep']++;
        }else{
            $lastVal=$data[$y][$offices_used[$i]];
            $buf[]=array('data'=>$lastVal,'rep'=>1);
        }

        foreach($buf as $arr){
            echo '<td'.($arr['rep']>1?' colspan="'.$arr['rep'].'"':'') . '>'.$arr['data'].'</td>';
        }


        }   echo '</tr>';
    } // END FOR 
对于($y=$year\u max;$y>=$year\u min;$y--){
回显“.$y.”;
$lastVal='';
$buf=数组();
对于($i=0;$i$lastVal,'rep'=>1);
}
外汇($buf作为$arr){
回显'.$arr['data'].';
}
}回声';
}//结束
注意:代码错误已更正


rowspan代码:(我认为它可以比这更容易下降。但我已经尽力做好了)

$buf=array();
对于($i=0;$i=$year\u min;$y--){
(!isset($data[$y][$offices\u used[$i]])?($data[$y][$offices\u used[$i]]='');//我不确定这是否真的需要
如果($lastVal==$data[$y][$offices\u used[$i]])){
$buf[$i][count($buf[$i])-1]['rep']++;
}否则{
$lastVal=$data[$y][$U使用的办公室[$i]];
$buf[$i][$year\u max-$y]=数组('data'=>$lastVal,'rep'=>1);
}
}
}
$mtemp=$year\u max;
foreach($buf作为$row){
回显“.$mtemp.”;
foreach(行作为$rec){
回显'.$rec['data'].';
}
$mtemp--;
回声';
}
真希望能工作

注意事项:更正

使用以下方法解决:

############################################################################
######## PRINT OUT TABLE WITH YEARS AND OFFICES PLUS NAMES IN CELLS ########    
for ($y = $year_max; $y >=$year_min; $y--){ // Loop through years
    echo '<tr><th>'.$y.'</th>';

    for ($i = 0; $i<count($offices_used); $i++){

        if (isset($data[$y][$offices_used[$i]])){

            $rowz =1;


            if(!($data[$y][$offices_used[$i]] == $data[($y+1)][$offices_used[$i]])){

                while ($data[$y][$offices_used[$i]] == $data[($y-$rowz)][$offices_used[$i]]) $rowz ++; 
                echo '<td rowspan = "'.$rowz.'">'.$data[$y][$offices_used[$i]].'</td>';
            }

        } // END IF

        else {
            echo '<td align = "center"> - </td>';
        } // END ELSE

        }   echo '</tr>';  // End row of current year

} // END FOR Loop through years 
echo '</table>';
############################################################################
########打印出带有年份和办公室以及单元格中名称的表格
对于($y=$year\u max;$y>=$year\u min;$y--){//循环年份
回显“.$y.”;

对于($i=0;$i我几天前写了一个库,可以处理这些东西。
看看

该库的好处是,您不需要考虑html中单元格的顺序,也不需要考虑由于rowspan或colspan而需要跳过哪些单元格。相反,您只需“绘制”网格中任意位置的单元格,具有所需的行跨度或列跨度。您只需指定rospan/colspan区域的第一行和最后一行以及列名,而不是为行跨度或列跨度指定数字

例如

下面是一些代码,它们在这个库的帮助下创建了您想要的表。您仍然需要一些逻辑来计算时间间隔,但至少不需要担心html表的完整性

(这个问题由来已久,但可能仍有人在谷歌上遇到过。所以我希望它可能有用。)


好的,请检查我的答案。我想你的意思是
colspan
。检查。可能差不多,但还没有。我想我已经掌握了这个概念。谢谢。以下是我想要的:以及我在实现你的建议时得到的:我无法打开你的页面。你能将HTML粘贴出来作为jsfiddle吗?这个链接是为哪一个链接提供的?新的??它实现了什么您提供了。这就是我的目标。(以前从未使用过JSFIDLE-有趣…)
$buf=array();
for ($i = 0; $i<count($offices_used); $i++){// this loop makes fill $buf
    $lastVal='';
    for ($y = $year_max; $y >=$year_min; $y--){
        (!isset($data[$y][$offices_used[$i]])?($data[$y][$offices_used[$i]]=''):'');// Im not sure that is really needed
        if($lastVal==$data[$y][$offices_used[$i]]){
            $buf[$i][count($buf[$i])-1]['rep']++;
        }else{
            $lastVal=$data[$y][$offices_used[$i]];
            $buf[$i][$year_max-$y]=array('data'=>$lastVal,'rep'=>1);
        }
    }
}
$mtemp=$year_max;
foreach($buf as $row){
    echo '<tr><th>'.$mtemp.'</th>';
    foreach($row as $rec){
        echo '<td'.($rec['rep']>1?' rowspan="'.$rec['rep'].'"':'') . '>'.$rec['data'].'</td>';
    }
    $mtemp--;
    echo '</tr>';
}
############################################################################
######## PRINT OUT TABLE WITH YEARS AND OFFICES PLUS NAMES IN CELLS ########    
for ($y = $year_max; $y >=$year_min; $y--){ // Loop through years
    echo '<tr><th>'.$y.'</th>';

    for ($i = 0; $i<count($offices_used); $i++){

        if (isset($data[$y][$offices_used[$i]])){

            $rowz =1;


            if(!($data[$y][$offices_used[$i]] == $data[($y+1)][$offices_used[$i]])){

                while ($data[$y][$offices_used[$i]] == $data[($y-$rowz)][$offices_used[$i]]) $rowz ++; 
                echo '<td rowspan = "'.$rowz.'">'.$data[$y][$offices_used[$i]].'</td>';
            }

        } // END IF

        else {
            echo '<td align = "center"> - </td>';
        } // END ELSE

        }   echo '</tr>';  // End row of current year

} // END FOR Loop through years 
echo '</table>';
$table->td(['2011', '2013'], 'Secretary', 'Eric Holmquist');
<?php

require_once __DIR__ . '/vendor/autoload.php';

$data = array(
  '2013' => array(
    'President' => 'John Mills',
    'Internal VP' => 'Virgil Bagdonas',
    'External VP' => 'Reid Gilmore',
    'Treasurer' => 'Todd Heino',
    'Secretary' => 'Eric Holmquist',
    'Newsletter' => 'Art Bodwell',
    'Webmaster' => 'Dave Eaton',
    'Photographer' => 'Rick Angus',
    'Video Librarian' => 'Mike Peters',
    'Store Manager' => 'Kevin Nee',
  ),
  '2012' => array(
    'President' => 'Dave Eaton',
    'Internal VP' => 'Jim Metcalf',
    'External VP' => 'Reid Gilmore',
    'Treasurer' => 'Mike Peters',
    'Secretary' => 'Eric Holmquist',
    'Newsletter' => 'Art Bodwell',
    'Webmaster' => 'Dave Eaton',
    'Photographer' => 'Peter Wilcox',
    'Video Librarian' => 'Ray Asselin',
    'Store Manager' => 'Joe Giroux',
  ),
  '2011' => array(
    'President' => 'Charlie Croteau',
    'Internal VP' => 'Reid Gilmore',
    'External VP' => 'Rick Angus',
    'Treasurer' => 'Mike Peters',
    'Secretary' => 'Eric Holmquist',
    'Newsletter' => 'Ron Rocheleau',
    'Webmaster' => 'Dave Eaton',
    'Photographer' => 'Peter Wilcox',
    'Video Librarian' => 'Ray Asselin',
    'Book Librarian' => 'Roger Boisvert',
    'Store Manager' => 'Mike Smith',
  ),
);

// Collect positions to build table columns.
$positions = [];
foreach ($data as $year => $yearData) {
  foreach ($yearData as $position => $person) {
    $positions[$position] = TRUE;
  }
}
$positions = array_keys($positions);

// Define table columns.
$table = (new \Donquixote\Cellbrush\Table())
  ->addColName('year')
  ->addColNames($positions)
;

// Create thead section.
$headRow = $table->thead()->addRow('head');
$headRow->th('year', 'Year');
foreach ($positions as $position) {
  $headRow->th($position, $position);
}

// Create table rows with labels based on year.
$years = array_keys($data);
$table->addRowNames($years);
foreach ($years as $year) {
  $table->th($year, 'year', $year);
}

// Fill table cells in each column.
foreach ($positions as $position) {
  $periodPerson = NULL;
  $periodFirstYear = NULL;
  $periodLastYear = NULL;
  $column = $table->colHandle($position);
  foreach ($years as $year) {
    $person = isset($data[$year][$position])
      ? $data[$year][$position]
      : '-';
    if (!isset($periodFirstYear)) {
      // First year.
      $periodFirstYear = $year;
    }
    elseif ($person !== $periodPerson) {
      // Add a table cell with rowspan.
      $column->td([$periodFirstYear, $periodLastYear], $periodPerson);
      $periodFirstYear = $year;
    }
    $periodPerson = $person;
    $periodLastYear = $year;
  }
  if (isset($periodFirstYear)) {
    // Add a table cell with rowspan.
    $column->td([$periodFirstYear, $periodLastYear], $periodPerson);
  }
}

print $table->render();