如何以行和列的组合显示数据?

时间:2018-04-23 07:39:26

标签: php mysql codeigniter

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

输出:

 NAME | Test | test2 | test3|
 raj  | pass |       | fail |
 user | fail | pass  |      |
 user1|      |       | pass |

下面是数据数组

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)

查看:

下面的代码是从数组

中显示表中的数据
<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>

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

    NAME |      | Test|test2|test3|
    raj  | pass |     |     |
    user | fail |     |     |
   user1 | fail |     |     |
    raj  | fail |     |     |
    user | pass |     |     |

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

3 个答案:

答案 0 :(得分:1)

添加了提取测试标题的部分。并修复了循环,首先显示包含测试标题的标题标题行。然后添加后续循环以将结果放入相应的测试列。

您可以尝试以下代码。

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

<强>输出:

enter image description here

答案 1 :(得分:0)

以下代码首先创建一个二维空网格。这是通过获取一系列独特课程和员工并使$grid为每位员工和每门课程设置一个插槽来完成的。然后,它只是通过原始数据并将标记放入每个插槽的情况。

$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'
    )
);


$tests = array_unique(array_column($recordss, 'test_title'));
$testDummy = array_fill(0, count($tests), "");
$testBlank = array_combine($tests, $testDummy);
$employees = array_unique(array_column($recordss, 'firstname'));
$grid = array();
foreach ( $employees as $employee ){
    $grid[$employee] = $testBlank;
}

foreach ( $recordss as $record )    {
    $grid[$record['firstname']][$record['test_title']] = $record['submittimestamp'];
}

print_r($grid);

使用测试数据,输出为......

Array
(
    [raj] => Array
        (
            [tofel] => pass
            [Test 1] => 
            [test2] => 
        )

    [user] => Array
        (
            [tofel] => 
            [Test 1] => fail
            [test2] => 
        )

    [user1] => Array
        (
            [tofel] => 
            [Test 1] => 
            [test2] => pass

)

)

您只需要一次处理每个$grid行,将其换行为您的格式。

<强>更新

查看代码...

<thead>
<tr>
<td>Employe Name</td>
<?php   
    foreach ($tests as $test)   {
            echo "<td>".ucfirst($test)."</td>";
    }?>
</tr>
</thead>
<tbody>
<?php foreach ($grid as $name => $line) {  ?>
    <tr>
        <td><a href="<?php echo base_url();?>welcome/employee/<?php echo $name;?>"><?php echo ucfirst($name); ?></td>
        <?php                                    
        foreach ( $line as $testResult )    {
            echo "<td>".ucfirst($testResult)."</td> ";
        } ?>

    </tr>
   <?php } ?>

</tbody>                    
</table>

答案 2 :(得分:0)

为什么您的输出不是您想要的,因为您没有检查数据字段,而是按垂直而不是水平打印数据。请查看下面的代码了解详细信息。

数组数据:

$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'
        ),
    );

过滤名称列的唯一名称,并过滤所有测试名称列的唯一测试名称。

$uniq_rec = array_unique($recordss,SORT_REGULAR);
$uniq_name = array_unique(array_column($recordss, 'firstname'));
$uniq_test = array_unique(array_column($recordss, 'test_title'));

这里是代码视图。

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

希望这对你有所帮助。 祝你好运。