简体   繁体   中英

MySQL Data in the PHP Array

I have a table as follows that is used to store details of employees:

+---------+---------+------------------+
| of_code |  name   | employment_status |
+---------+---------+------------------+
|       1 | Jhon    |                1 |
|       2 | Ram     |                1 |
|       3 | Edward  |                3 |
|       4 | William |                2 |
+---------+---------+------------------+

This is one of the options in my Codeigniter project.

Desired output

I want to show the employment_status in separate columns in the view as per the value in it. If employment_status = 1, it should be in the first column. If employment_status = 2, it should be in the second column. If employment_status = 3, it should be in the the column and so on.

Controller (Relevant Part)

$where = NULL;
$this->data['officerList'] = $this->Officer_model->officerSummary($where);

Model

function officerSummary($where){
    $q = $this->db->query("SELECT 
    of_code, name, employment_status as cnt 
    from tbl_officer $where     

    ");
        if ($q->num_rows() > 0) {
            return $q->result();

        }
    }

View (Relevant Part)

<?php
$officerLists = [];
foreach ($officerList as $row) {
    if (!in_array($row->cnt, $officerLists)) {
        $officerLists[] = $row->cnt;
    }
}
?>

<div class="table-responsive" id="datatable">

                <table id="ExData" cellpadding="0" cellspacing="0" border="0"
                       class="table table-bordered table-condensed table-hover table-striped reports-table">
                    <thead id="th">
                    <tr class="" style="background-color: #d966ff !important;">
                        <th>#</th>

                        <th style width="5%" class="text-center"><font size="1"> Code</th>
                        <th style width="15%" class="text-left"><font size="1">Name</th>                                
                        <th class="text-center" colspan="<?= count($officerLists) ?>"><font size="1">Employment Status</th>         

                    </tr>

                    </thead>
                    <tbody>

                    <?php

                    $count = [];                   
                    if(!empty($officerList)) {
                        foreach ($officerList as $rows) {
                            $total += $rows->cnt;

                            $c++;
                            ?>
                            <tr>

                                <td><font size="1"><?= $rows->of_code ?></td>
                                <td><?= $rows->name ?></td>                               

                                <?php
                                foreach ($officerLists as $value) {
                                if ($rows->cnt == $value) {
                                if (isset($count[$value]))
                                    $count[$value] = $count[$value] + $rows->of_code;
                                else
                                    $count[$value] = $rows->of_code;
                                echo "<td class='text-center'></td>";
                            } else
                                echo "<td class='text-center'>-</td>";                          
                             }                      

                        }

                        ?>

                    </tbody>

                </table>

The function outs three columns correctly, but with no values. Only '-' shows non-relevant places in the column as follows. 就业状况

What may be going wrong? Can anyone help?

Maybe using a SELECT - CASE structure can be your option

A query in this way

SELECT of_code,
       name,
       CASE WHEN employment_status = 1 THEN employment_status AS StatusA,
       CASE WHEN employment_status = 2 THEN employment_status AS StatusB,
       CASE WHEN employment_status = 3 THEN employment_status AS StatusC
FROM yourTbale;
  • Every CASE will verify if the status is 1 or 2 or 3
  • Th previous step will generate a new column with a given alias
  • Because we need the status value as cell value in our result I put it again after the THEN clause

If this work, you only will need to adapt it in CI sintax

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM