【问题标题】:For loop to create extra columnsFor 循环创建额外的列
【发布时间】:2013-07-23 07:10:42
【问题描述】:

这段代码显示了一个基本的树形结构括号,我正在尝试完成它以现在执行以下操作:-

  1. 显示位置:

    style="border-right:2px solid #000;"

    我正在尝试为每一行使用此边框,但此脚本不会在团队/种子之间创建行来定义边框,我正在尝试为每个 TEAM 1 到 TEAM 的每一行显示边框右2 等以及所有其他回合 TEAM 1 到 TEAM 2 等。

  2. PHP 错误报告正在返回以下行,在 } ELSE {? 之后还有一个未定义的偏移量?

    $line[$i] .= '<td align="center" style="border-right:2px solid #000;" colspan="2">vs</td>';
    

    3.

            $array = array('Team 1', 'Team 2', 'Team 3', 'Team 4', 'Team 5', 'Team 6', 'Team 7', 'Team 8');
            $j = count($array);
            for ($a=0; pow(2, $a) <= $j; $a++) //determine the highest power of 2 that will go into the array count
            {
                $y[$a] = 1;
                $maxpower = $a;
            }
            for ($i=1; $i < $j*2; $i++)
            {
                if($i % 2 != 0) //odd number rows for teams
                {
                    $line[$i] = '<td class="pf_title_bg" style="border-right:2px solid #000;">' . $array[($i-1)/2] . '</td>
                                 <td class="pf_content_bg" style="border-right:2px solid #000;">&nbsp;</td>';
                }
                else
                {
    
                    for($b=0; $b<=$maxpower; $b++)
                    {
    
                        $round = $b+1;              
                        $line2[$b] = ($b < $maxpower ? "<th colspan='2'>Round {$round}</th>" : "<th colspan='2'>Winner</th>");
    
                        if($i % pow(2, $b) == 0) //even rows for future rounds. every 2^1 rows for first winner, 2^2 for second winner, 2^3 for third and so on.
                        {
                            if($i % pow(2, $b+1) != 0) //does not divide by the next power of 2, so this must be the last available cell
                            {
                                $line[$i] .= '<td class="pf_title_bg" style="border-right:2px solid #000;">Team '.$b.'_'.($y[$b]++).'</td>
                                              <td class="pf_content_bg" style="border-right:2px solid #000;">&nbsp;</td>';
                            }
                            else //the input will be added in a future round
                            {
                                $line[$i] .= '<td align="center" style="border-right:2px solid #000;" colspan="2">vs.</td>';
                            }
                        }
                    }
                }
            }   
    
            //name="WIN'.$b.'_'.($y[$b]++).'                
    
            eval ("\$content = \"".$this->gettemplate("table_header")."\";");
    
            $content.="<thead><tr>";
    
            foreach($line2 as $col) 
            {
                $content.=$col;
            }
    
            $content.="</tr></thead>";
    
            foreach($line as $row)
            {
                $content.="<tbody><tr>{$row}</tr></tbody>";
            }
    
            eval ("\$content.= \"".$this->gettemplate("table_footer")."\";");
    

非常感谢任何帮助

【问题讨论】:

标签: php arrays loops for-loop html-table


【解决方案1】:

您没有正确构建表格;您在每一行周围都放置了一个 tbody 标签,从而使您的 HTML 无效。这对我有用:

<?php
$array = array('Team 1', 'Team 2', 'Team 3', 'Team 4', 'Team 5', 'Team 6', 'Team 7', 'Team 8');
            $j = count($array);
            for ($a=0; pow(2, $a) <= $j; $a++) //determine the highest power of 2 that will go into the array count
            {
                $y[$a] = 1;
                $maxpower = $a;
            }
            for ($i=1; $i < $j*2; $i++)
            {
                if($i % 2 != 0) //odd number rows for teams
                {
                    $line[$i] = '<td class="pf_title_bg" style="border-right:2px solid #000;">' . $array[($i-1)/2] . '</td>'.PHP_EOL.'<td class="pf_content_bg" style="border-right:2px solid #000;">&nbsp;</td>'.PHP_EOL;
                }
                else
                {

                    for($b=0; $b<=$maxpower; $b++)
                    {

                        $round = $b+1;
                        $line2[$b] = $b < $maxpower ? "<th colspan='2'>Round {$round}</th>".PHP_EOL : "<th colspan='2'>Winner</th>".PHP_EOL;

                        if($i % pow(2, $b) == 0) //even rows for future rounds. every 2^1 rows for first winner, 2^2 for second winner, 2^3 for third and so on.
                        {
                            if($i % pow(2, $b+1) != 0) //does not divide by the next power of 2, so this must be the last available cell
                            {
                                $line[$i] .= '<td class="pf_title_bg" style="border-right:2px solid #000;">Team '.$b.'_'.($y[$b]++).'</td>'.PHP_EOL.'<td class="pf_content_bg" style="border-right:2px solid #000;">&nbsp;</td>';
                            }
                            else //the input will be added in a future round
                            {
                                $line[$i] .= '<td align="center" style="border-right:2px solid #000;" colspan="2">vs.</td>'.PHP_EOL;
                            }
                        }
                    }
                }
            }

            //name="WIN'.$b.'_'.($y[$b]++).'

            //eval ("\$content = \"".$this->gettemplate("table_header")."\";");

            $content="<table><thead><tr>";

            foreach($line2 as $col)
            {
                $content.=$col;
            }

            $content.="</tr></thead><tbody>".PHP_EOL;

            foreach($line as $row)
            {
                $content.="<tr>{$row}</tr>".PHP_EOL;
            }

            $content.='</tbody></table>';

            //eval ("\$content.= \"".$this->gettemplate("table_footer")."\";");
            echo $content;

?>

【讨论】:

  • 非常感谢您的更新,但我仍然希望在每个团队 1 与团队 2 之间的每一行中添加右边框样式,我不知道该怎么做?
  • 我不确定我明白你的意思,因为他们对我有边境权利。如果您需要找到&lt;td&gt;,则将类添加到您输入的类中以查明生成它的位置,即&lt;td class="i_mod_2_not_zero"&gt;
  • 你检查过你没有冲突的css吗?
  • 基本上是脚本产生的,看看图片:-team-x1.co.uk/script.png 你可以看到每个团队 1 和团队 2 之间的每一行都没有实现border-right。你能理解什么我想做什么?谢谢大家!
  • 我没有。你能圈出边界要去的地方吗?对我来说,Team 1 vs. Team 2 意味着第一列确实有线一直向下运行。
猜你喜欢
  • 2021-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多