首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何结合行和列来显示数据?

如何结合行和列来显示数据?
EN

Stack Overflow用户
提问于 2018-04-23 07:39:26
回答 2查看 124关注 0票数 0

嗨,我有一个数组,我想在一个表中以行和列的组合显示数组,如下面的预期输出表。所有值都是在空列中获取所有值,而不是在测试中。

输出:

代码语言:javascript
复制
 NAME | Test | test2 | test3|
 raj  | pass |       | fail |
 user | fail | pass  |      |
 user1|      |       | pass |

下面是数据数组

代码语言:javascript
复制
array(size = 8)
'firstname'       => string 'raj' (length = 20)
'test_title'      => string 'tofel' (length = 5)
'submittimestamp' => string 'pass' (length = 19)
9 =>
array(size = 8)
'firstname'       => string 'user' (length = 5)
'test_title'      => string 'Test 1' (length = 6)
'submittimestamp' => string 'fail' (length = 19)
10 =>
array(size = 8)
'firstname'       => string 'user1' (length = 7)
'test_title'      => string 'test2' (length = 5)
'submittimestamp' => string 'pass' (length = 19)

视图:

下面的代码用于从数组中显示表中的数据

代码语言:javascript
复制
<thead>
    <tr>
        <th>Employe Name</th>
        <?php   if(count($recordss))
                               {                               
                     foreach ($recordss as $records) 
                               { 
                                $records = (object)$records;
                                $title=$records->test_title;
                              ?>
        <td>
            <?php  echo ucfirst($title); ?>
        </td>
        <?php  }}?>
    </tr>

</thead>
<tbody>
            <?php 

                  if(count($recordss)){                               
                  foreach ($recordss as $records) { 
                    $records = (object)$records;               
                  ?>
    <tr>
        <td><a href="<?php echo base_url();?>welcome/employee/<?php echo $records->usr_id;?>"><?php echo ucfirst($records->firstname); ?></td>                                    
           <td><?php echo ucfirst($records->submittimestamp); ?></td> 

    </tr>
   <?php } } ?>

</tbody>                    
</table>

我已经试过了,我的输出如下:

代码语言:javascript
复制
    NAME |      | Test|test2|test3|
    raj  | pass |     |     |
    user | fail |     |     |
   user1 | fail |     |     |
    raj  | fail |     |     |
    user | pass |     |     |

所有的值都在额外的列中,但没有经过测试。请帮我解决这个问题谢谢。我需要像上面的输出表那样显示数据。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-04-23 08:13:25

增加了提取测试标题的部分。并修正了首先显示标题行的循环,其中包含测试标题。然后添加后续循环,将结果放置到相应的测试列中。

您可以尝试下面的代码。

代码语言:javascript
复制
<table border="1">
<thead>
  <?php 
    $recordss = array(
        8 => array(
            'firstname' => 'raj',
            'test_title' => 'tofel',
            'submittimestamp' => 'pass'
        ),
        9 => array(
            'firstname' => 'user',
            'test_title' => 'Test 1',
            'submittimestamp' => 'fail'
        ),
        10 => array(
            'firstname' => 'user1',
            'test_title' => 'test2',
            'submittimestamp' => 'pass'
        ),

        12 => array(
            'firstname' => 'john',
            'test_title' => 'tofel',
            'submittimestamp' => 'fail'
        )
    );  

    if(count($recordss)) {  
        $titles = array_unique(array_column($recordss, 'test_title'));
        array_unshift($titles , 'Name');

        echo "<tr>";
        foreach($titles as $title){
            echo "<th>$title</th>";
        }                       
        echo "</tr>";

        foreach ($recordss as $records) {
            echo "<tr>";
            for($i = 0; $i < count($titles); $i++) {
                if($i == 0){
                    echo "<td>".$records['firstname']."</td>";
                }
                else {
                    if( $records['test_title'] == $titles[$i] )
                        echo "<td>".( $records['submittimestamp'])."</td>";
                    else 
                        echo "<td></td>";
                }
            }
            echo "</tr>";
            ?>
            <?php
        }
    }
  ?>   
</thead>
 </table>

输出:

票数 1
EN

Stack Overflow用户

发布于 2018-04-23 10:29:03

为什么您的输出与您希望的不一样,因为您没有检查数据字段,并且您以垂直而不是水平的方式打印数据。请查看下面的代码以获得详细信息。

数组数据:

代码语言:javascript
复制
$recordss = array(
            8 => array(
                'firstname' => 'raj',
                'test_title' => 'tofel',
                'submittimestamp' => 'pass'
            ),
            9 => array(
            'firstname' => 'user',
            'test_title' => 'Test 1',
            'submittimestamp' => 'fail'
            ),
            10 => array(
                'firstname' => 'user1',
                'test_title' => 'test2',
                'submittimestamp' => 'pass'
        ),
        11 => array(
                'firstname' => 'bro',
                'test_title' => 'tofel',
                'submittimestamp' => 'pass'
            ),
            12 => array(
            'firstname' => 'bro',
            'test_title' => 'Test 1',
            'submittimestamp' => 'pass'
        ),
        13 => array(
            'firstname' => 'raj',
            'test_title' => 'Test 1',
            'submittimestamp' => 'pass'
        ),
        14 => array(
            'firstname' => 'user1',
            'test_title' => 'tofel',
            'submittimestamp' => 'pass'
        ),
    );

为名称列筛选唯一名称,为所有测试名称列筛选唯一测试名称。

代码语言:javascript
复制
$uniq_rec = array_unique($recordss,SORT_REGULAR);
$uniq_name = array_unique(array_column($recordss, 'firstname'));
$uniq_test = array_unique(array_column($recordss, 'test_title'));

这里是代码视图。

代码语言:javascript
复制
<table border="1">
    <thead>
    <tr>
        <th>Employe Name</th>
        <?php
        foreach ($uniq_test as $row) {
        ?>
            <th><?php echo $row?></th>
        <?php
        }
        ?>
    </tr>
    </thead>
    <tbody>
        <?php
            foreach ($uniq_name as $row) {
        ?>
            <tr>
            <td><?php echo $row?>
            <?php
                foreach ($uniq_test as $uniq) {;
                    $status = "";
                    foreach ($recordss as $rec) {
                        if($uniq == $rec['test_title'] && $rec['firstname'] == $row){
                             echo "<td>".$rec['submittimestamp']."</td>";
                            $status = "true";
                        }
                    }
                    if($status != "true"){
                        echo "<td></td>";
                    }
                    $status = "";
                }
            ?>
        </tr>
        <?php } ?>
    </tbody>
</table>

希望这能帮到你。祝好运。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49975733

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档