duanhe0817825 2013-07-24 01:19 采纳率: 100%
浏览 44
已采纳

HTML_Table返回不正确的输出

I'm trying to use PEAR::HTML_Table package to generate a table from multi-dimensional array, $data.

This is a dump of $data:

Array
(
    [0] => Array
        (
            [0] => Office
            [1] => Canvasser
            [2] => Fundraising Hrs
            [3] => PAC/Hr no PFU
            [4] => PAC $ no PFU
        )
    [1] => Array
        (
            [0] => TBS1
            [1] => Vatcher, Georgia
            [2] => 29
            [3] => 8.295
            [4] => 481
        )
)

EDIT: The first array is the row of my THEAD tags and any additional arrays will be rows in the TBODY tags.

And this is my code:

$data = $worksheet->toArray('', true, false, false); // PHPExcel

// build table
$table = new HTML_Table(array('class' => 'dt'), 0, true);
$thead =& $table->getHeader();
$tbody =& $table->getBody();

// loop through rows
for ($r = 0, $lr = count($data); $r < $lr; $r++) {
    // loop through columns
    for ($c = 0, $lc = count($data[$r]); $c < $lc; $c++) {
        if ($r == 0) {
            $thead->setCellContents($r, $c, $data[$r][$c]);
        } else {
            $tbody->setCellContents($r, $c, $data[$r][$c]);
        }
    }
}

// output html
echo $table->toHtml();

When it outputs the HTML, it returns a table with an extra row having blank cells in the TBODY tags. I cannot seem to figure out why it is doing that. It is important for me to get the right output because I am sending this output back to a JavaScript file for further processing.

How can I fix this?

  • 写回答

1条回答 默认 最新

  • dtxa49711 2013-07-24 07:12
    关注

    You need to reset the counter for the row in the tbody.

    The first body cell gets coordinates (1,0), but should have (0,0) - the row position begins at 0, not at 1. So you implicit tell HTML_Table to add a clear row before your first row.

    To fix it, use $r - 1 instead of $r in the else block. And learn about foreach().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有偿,学生成绩信息管理系统
  • ¥15 Arduino电机和openmv连接异常
  • ¥15 Arcgis河网分级报错
  • ¥200 java+appium2.1+idea
  • ¥20 请帮我做一个EXE的去重TXT文本
  • ¥15 工价表引用工艺路线,应如何制作py和xml文件
  • ¥15 根据历史数据,推荐问题类型
  • ¥15 需要仿真图,简单的二阶系统实例
  • ¥15 stm32光控照明仿真
  • ¥15 使用人工智能的方法生成满足一定统计参数要求的随机数序列