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

7xllpg7q  于 2021-06-21  发布在  Mysql
关注(0)|答案(3)|浏览(286)

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

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

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

zpjtge22

zpjtge221#

添加了提取测试标题的部分。并修复了循环以首先显示包含测试标题的标题行。然后添加后续循环,将结果放置到相应的测试列中。
你可以试试下面的代码。

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

输出:

b4wnujal

b4wnujal2#

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

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

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

lmvvr0a8

lmvvr0a83#

下面的代码首先创建一个二维空网格。这是通过得到一个独特的课程和员工名单,并制定 $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

)

)

你只需要把它 Package 成你的格式,然后处理每一个 $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>

相关问题