如何使用php数组制作html合并行表

时间:2014-01-07 18:02:28

标签: php arrays html-table

我从mysql数据库获得以下数组,在获取所有数据后,我应创建一个表来显示所有值。

Array

(
    [0] => Array
        (
            [groupNo] => 1001
            [name] => james
        )

    [1] => Array
        (
            [groupNo] => 1002
            [name] => chen
        )

    [2] => Array
        (
            [groupNo] => 1002
            [name] => ash
        )
    [3] => Array
        (
            [groupNo] => 1001
            [name] => mark
        )

)

我目前的表格就像这样:

Group Number | Name          | Action    |          
-------------+------+---------------------
1001         | James         |           |
------------+----------------+------------
1002         | chen          |           |
-------------+---------------+------------
1002         | ash           |           |
-------------+---------------+------------
1001         | mark          |           |
-------------+---------------+------------

但我想要的是我的桌子看起来如下:

Group Number | Name          | Action    |          
-------------+------+---------------------
1001         | James         |           |
             +---------------+------------
             | Mark          |           |
-------------+----------------------------
1002         | chen          |           |
             +----------------------------
             | ash           |           |
-------------+----------------------------

以下是我的代码:

<?php
if (count($sharingGroup) > 0) {
    for ($i = 0; $i < count($sharingGroup); $i++) {
        $sharingGroupRecord = $sharingGroup[$i];
        ?>
        <tr>
        <td>' . $sharingGroupRecord ['groupNo'] .'</td>
        <td>' . $sharingGroupRecord ['name'] .'</td>
        <td><a name="action" href="#">Action<span>
        </span></a>
</tr>

任何人都可以帮我解决这个问题。

4 个答案:

答案 0 :(得分:1)

试试这个:

<?php
$transpose=array();
if (count($sharingGroup) > 0) {
    for ($i = 0; $i < count($sharingGroup); $i++) {
        $sharingGroupRecord = $sharingGroup[$i];
        $transpose[$sharingGroupRecord['groupNo']][]=$sharingGroupRecord['name'];
    }
print_r($transpose);
$output="<table>";
foreach ($transpose as $groupNo => $group){
    $output.="<tr><td>$groupNo</td><td>";
    foreach ($group as $name){
        $output.="$name<br />";
    }
    $output.="</td></tr>";
}
$output.="</table>";

echo $output;

有更多行..

<?php
$transpose=array();
if (count($sharingGroup) > 0) {
    for ($i = 0; $i < count($sharingGroup); $i++) {
        $sharingGroupRecord = $sharingGroup[$i];
        $transpose[$sharingGroupRecord['groupNo']][]=$sharingGroupRecord['name'];
    }
print_r($transpose);
$transpose_again=array();
foreach ($tranpose as $groupNo=>$group){
    foreach($group => $name){
        $transpose_again[][$groupNo][]=$name;
    }
}
// Now you have a row numbers that is sorted by the groupNo.. I will stop here and let you figure out how to present it and do the rowspan if you really want to..

答案 1 :(得分:1)

首先,您需要重新创建数组

$newArray = array();

foreach($sharingGroup as $item) {
    $newArray[$item['groupNo']][] = $item['name'];
}

它会产生这个:

Array
(
    [1001] => Array
        (
            [0] => james
            [1] => mark
        )

    [1002] => Array
        (
            [0] => chen
            [1] => ash
        )

)

然后循环遍历newArray

if (count($newArray) > 0) {

    $html = '';

    foreach($newArray as $key => $val) {
       $html .= "<tr>\r\n";    

       $html .= "<td rowspan='".count($val)."'>{$key}</td>\r\n";

       foreach($val as $key => $td) {
           if($key>0) {
              $html.= "<tr>";
           }
           $html .= "<td>{$td}</td>\r\n";
           $html .= "<td>Action</td>\r\n";
           $html .= "</tr>\r\n";
       }        
    }
}

您将得到this

这是code所有在一个地方

答案 2 :(得分:0)

假设以下是您的输入数组

$array = array(
    array(
            "groupNo" => 1001,
            "name" => "james"
        ),
    array(
            "groupNo" => 1002,
            "name" => "chen"
        ),
    array(
            "groupNo" => 1002,
            "name" => "ash",
        ),
    array
        (
            "groupNo" => 1001,
            "name" => "mark"
        ),
    array
        (
            "groupNo" => 1003,
            "name" => "marco"
        ),
);

然后你可以做到以下几点:

$newArray =array();
foreach($array as $arr){
    $newArray[$arr['groupNo']][] = $arr["name"];
}
unset($array); // unset the unwanted array.

用于打印表格

<table border="1">

<?php if(!empty($newArray)){
   foreach($newArray as $key=>$val){
    echo "<tr>";
        echo "<td rowspan='".sizeof($val)."'>".$key."</td>";
        echo "<td>".$val[0]."</td>";
    echo "</tr>";   
    for($i=1;$i<sizeof($val);$i++){
        echo "<tr><td>".$val[$i]."</td></tr>";
    }

   }

} ?>
</table>

答案 3 :(得分:0)

用于创建新数组的PHP逻辑,将单个组中的所有组成员作为数组的单个元素

<?php
    $group=Array(0=>array('groupNo' => 1001, 'name' => "james"), 1=>array('groupNo' => 1002, 'name' => "chen"), 2=>array('groupNo' => 1002, 'name' => "ash"), 3=>array('groupNo' => 1001, 'name' => "mark"));
    $new_group=array();
    for($i=0; $i < count($group); $i++)
    {
        $new_group[$i]=array($group[$i]['groupNo']);
        for($j=0; $j < count($group); $j++)
        {
            if($new_group[$i][0]==$group[$j]['groupNo'])
            {
                $new_group[$i][]=$group[$j]['name'];
                array_splice($group, $j, 1);
                $j--;
            }
        }
    }
    print_r($new_group);
?>

HTML表格结构(您需要rowspan属性)

<table id=t1 border="2">
    <?php
        if(count($new_group)>0)
            echo "<tr>
                        <th>Group Number
                        <th>Name
                        <th>Action
            </tr>";
        foreach($new_group as $grouping)
        {
            $count=count($grouping)-1;
            echo "<tr>
                    <td rowspan=".$count.">".$grouping[0];
            for($j=1; $j <= $count; $j++)
            {
                echo "<td>".$grouping[$j];
                echo "</tr>";
                if($j<$count)
                    echo "<tr>";
            }
        }
    ?>
</table>

<强> JSfiddle DEMO O/P