如何合并表行与php数组? [英] How to merge table row with php array?

查看:143
本文介绍了如何合并表行与php数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果日期具有相同的ID,我想合并teble行。我有这样的数组日期:

I want to merge the teble row if the date have an same id's. I have some array date like this :

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为:

Then I tried to group its data into the same 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的数据)有显示一个错误,因为未定义的偏移量。所以我如何添加一些处理程序,如果只有一个孩子的数据。有我想要的结果:

There is no problem if the data have at least two child, but if the date only have one child (see data with id's 7) there is show an error because Undefined offset. So how I add some handler if there is only one child on the data. There is my desired result :

请看这张图片(对不起,我不能使用stackoverflow菜单附加图像):
http://i57.tinypic.com/mhaj69.jpg

please see this image (sorry I cant attach image using stackoverflow menu) : http://i57.tinypic.com/mhaj69.jpg

推荐答案

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

I use the 'read ahead' technique for processing nested loops. It does mean that 'foreach' loops cannot be used as the next record must be read as soon as the current one has been processed. Basically, the last action you do in the loop is read the next record as you are setting it up for the next iteration. Note, you never test when to print a record as that is decided by the structure of the groups. The code loops are the same as the structure of the groups in the data

A'组'全部具有相同 id

我假设content和act对于组中的每个条目是相同的。

I assume that the 'content' and 'act' are identical for each entry in the group.

编辑为将rowspan属性添加到相应的td标记。我怀疑css可能会更容易在这一点上。

Edited to add 'rowspan' attributes to the appropriate 'td' tags. I suspect css may be easier at this point.

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

The issue is that the group cannot be displayed until it is known how many entries are in it.

因此,我'缓冲'属于数组中的一个组的所有记录。在组的末尾,它将在html中显示相应的rowspan属性。

So, i 'buffer' all the records belonging to a group in an array. at the end of the group, it is displayed with the appropriate 'rowspan' attributes in the html.

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

It is tested on PHP 5.3.18. It includes test data.

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

这篇关于如何合并表行与php数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆