【问题标题】:How to merge table row with PHP array?如何将表格行与 PHP 数组合并?
【发布时间】:2014-07-24 14:20:24
【问题描述】:

如果日期具有相同的 id,我想合并表格行。我有一些这样的数组数据:

Array
(
    [0] => Array
        (
            [id] => 2
            [date] => 05/13/2014
            [content] => some contents 1
            [act] => act1 act2 act3
        )
    [1] => Array
        (
            [id] => 2
            [date] => 05/28/2014
            [content] => some contents 1
            [act] => act1 act2 act3
        )
    [2] => Array
        (
            [id] => 7
            [date] => 06/04/2014
            [content] => some contents 2
            [act] => act1 act2 act3
        )
)

然后我尝试将其数据分组到相同的 id by :

            if($queryx = $this->mymodel->myfunction($par)){
                $datax = $queryx;
            }           
            $i = 0;
            foreach ($datax  as $idx => $val) {
                $dates[$val['id']][$i] = $val['date'].",".$val['id'];
                $contn[$val['id']][$i] = $val['content'];

                $i++;
            }

然后:

            $j = 0; $y = 0;
            echo "<table border='1'>\r\n";
            foreach ($dates as $idx => $val) {
                $tmpexp = explode(',', $val[$j]);
                echo "<tr>\r\n";
                echo "<td rowspan='".count($val)."'>".$tmpexp[0]."</td>\r\n";
                foreach ($val as $idx1 => $val1) {
                    if($idx1 > 0)
                    {
                        echo "<tr>\r\n";
                    }
                    echo "<td>".$dates[$tmpexp[1]][$y]."</td>\r\n";
                    if($idx1 < 1)
                    {
                        echo "<td rowspan='".count($val)."'>".$contn[$tmpexp[1]][$y]."</td>\r\n";                       
                        echo "<td rowspan='".count($val)."'>act1 act2 act3</td>";
                        echo "</tr>\r\n";
                    }
                    $y++;
                }
                $j++;   
            }
            echo "</table>";

如果数据至少有两个孩子没有问题,但是如果日期只有一个孩子(参见 id 为 7 的数据),则会显示错误,因为未定义偏移量。那么如果数据上只有一个孩子,我如何添加一些处理程序。有我想要的结果:

请看这张图片:

【问题讨论】:

  • 请解释为什么 5/28 没有内容也没有行动?
  • 因为内容的 rowspan = 2。记住 5/13 和 5/28 具有相同的 id
  • 我明白了,所以您想跳过内容并在当前 id = 最后一个 id 时采取行动?为什么不能在迭代时就这样比较?
  • 为垂直格式添加了“rowspan”。

标签: php html mysql codeigniter html-table


【解决方案1】:

下面的代码会给你一个线索,你应该做什么,

$newArray = array();        
$counter=0;        
foreach($datax  as $key=>$val){        
    $newArray[$val['id']][$counter]['date'] = $val['date'];       
    $newArray[$val['id']][$counter]['content'] = $val['content'];        
    $newArray[$val['id']][$counter]['act'] = $val['act'];        
    $counter++;        
}        
$newArray = array_map('array_values', $newArray);

【讨论】:

    【解决方案2】:

    我使用“预读”技术来处理嵌套循环。这确实意味着不能使用“foreach”循环,因为必须在处理当前记录后立即读取下一条记录。基本上,您在循环中执行的最后一个操作是读取下一条记录,因为您正在为下一次迭代设置它。请注意,您永远不会测试何时打印记录,因为这是由组的结构决定的。代码循环与数据中组的结构相同

    “组”是所有具有相同id的记录。

    我假设组中每个条目的“内容”和“行为”都是相同的。

    已编辑以将“rowspan”属性添加到适当的“td”标签。我怀疑此时css可能更容易。

    问题是在知道其中有多少条目之前,无法显示该组。

    所以,我将属于一个组的所有记录“缓冲”在一个数组中。在组的末尾,它在 html 中以适当的“rowspan”属性显示。

    在 PHP 5.3.18 上测试。它包括测试数据。

    <?php /* Q24028866 */
    $testData = array(array('id' => 2, 'date' => '05/13/2014', 'content' => 'some contents 2', 'act' => 'act1 act2 act3'),
                      array('id' => 2, 'date' => '05/28/2014', 'content' => 'some contents 2',  'act' => 'act1 act2 act3'),
                      array('id' => 7, 'date' => '06/04/2014', 'content' => 'some contents 7',  'act' => 'act1 act2 act3'),
                      array('id' => 8, 'date' => '06/08/2014', 'content' => 'some contents 8',  'act' => 'act1 act2 act3'),
                      array('id' => 8, 'date' => '06/09/2014', 'content' => 'some contents 8',  'act' => 'act1 act2 act3'));
    ?>
    <!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
    <table border='1'>
    <thead><th>Date</th><th>Content</th><th>Act</th></thead>
    <?php
    // use 'read ahead' so there is always a 'previous' record to compare against...
    $iterContents = new \ArrayIterator($testData);
    $curEntry = $iterContents->current();
    
    while ($iterContents->valid()) { // there are entries to process
    
        $curId = $curEntry['id'];
    
        // buffer the group to find out how many entries it has...
        $buffer = array();
        $buffer[] = $curEntry;
    
        $iterContents->next(); // next entry - may be same or different id...
        $curEntry = $iterContents->current();
    
        while ($iterContents->valid() && $curEntry['id'] == $curId) {  // process the group...
            $buffer[] = $curEntry; // store all records for a group in the buffer
    
            $iterContents->next(); // next entry - may be same or different id...
            $curEntry = $iterContents->current();
        }
    
         // display the current group in the buffer...
         echo '<tr>';
         echo '<td>', $buffer[0]['date'], '</td>';
         $rowspan = count($buffer) > 1 ? ' rowspan="'. count($buffer) .'"' : '';
         echo '<td', $rowspan, '>', $buffer[0]['content'], '</td>',
               '<td', $rowspan, '>', $buffer[0]['act'], '</td>';
         echo '</tr>';
         for($i = 1; $i < count($buffer); $i++) {
              echo '<tr><td>', $buffer[$i]['date'], '</td>';
              echo '</tr>';
         }
    } ?>
    </table>
    </body>
    </html>
    

    【讨论】:

    • 嗨@Ryan Vincent,感谢您的工作。可能是有误会。请看我上面的更新图片。我的意思是,如果数据具有相同的 ID,则存在行跨度。所以,如果数据具有相同的 id --> 行跨越 id,内容 dan 行为,但不是日期,因为日期总是不同......对此有任何想法......谢谢......
    • 完美运行!非常感谢你@Ryan Vincent...太棒了..!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    相关资源
    最近更新 更多