【问题标题】:How to make html merge rows table using php array如何使用php数组制作html合并行表
【发布时间】:2014-01-25 13:09:45
【问题描述】:

我有以下从 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>

请任何人帮我解决这个问题。

【问题讨论】:

    标签: php arrays html-table


    【解决方案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 集中在一处

    【讨论】:

    • 谢谢,但它远非完美,没有保障措施,以防新阵列中只有一个孩子等
    • @Kostis 没关系。现在这就是我想要的。再次感谢! =)
    【解决方案2】:

    试试这个:

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

    【讨论】:

    • 感谢@MoeTsao,这几乎是我想要的,但是如何使名称列每个名称都有一行
    • 这是可行的,但代码会很快变得丑陋。您确定需要将它放在单独的行中吗?使用 css 呈现行之间的分隔符?
    • 是的,因为我想在每行名称中添加一些删除操作。我不在乎代码长什么样,我稍后会自定义它=)
    【解决方案3】:

    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

    【讨论】:

      【解决方案4】:

      假设以下是你的输入数组

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

      【讨论】:

        猜你喜欢
        • 2014-07-24
        • 2021-07-06
        • 1970-01-01
        • 2022-08-07
        • 1970-01-01
        • 2010-09-06
        • 1970-01-01
        • 1970-01-01
        • 2015-11-05
        相关资源
        最近更新 更多