我想按类别在两个不同的列中显示记录

时间:2019-01-29 13:23:04

标签: php mysql sql codeigniter

在我的表中,我有一个列voucher_category,其中我有两种(ENUM)类型的数据cash_receiptcash_payment。  而且我的表中还有另外一个Voucher_type列,其中我有两种类型的creditdebit。 我想在两个不同的列中以voucher_category的形式显示Voucher_type,如下表所示

 I want this type of columns

 Voucher No  Voucher_Category_By_Credit  Voucher_Category_By_Debit         
         1               cash payment 
         2                                         cash receipt


Currently I'am receiving like this bellow

 Voucher No      Voucher_Category           voucher_category_type         
       1            cash payment               credit
       2            cash receipt                debit

这是我的代码

Controller



$modResult  = $this->sendValues->receiving($data);

<tr>
  <th>Voucher No.</th>
  <th>Voucher_Category</th>
  <th>voucher_category_type</th>
</tr>

      <?php foreach($modResult as $voucher):?>
       <tr>
         <td><?php echo $voucher['voucher_no'];?></td>
         <td><?php echo $voucher['voucher_category'];?></td>
         <td><?php echo $voucher['voucher_category_type'];?></td>
        </tr>
       <?php endforeach; ?>


model


public function receiving($data){
        extract($data);

            $this->db->select('*');
            $this->db->from('ts_voucher');

          $this->db->where('voucher_category','cash_receipt');
          $this->db->or_where('voucher_category','cash_payment');

           $query=$this->db->get();
           return $query->result_array();

 }

1 个答案:

答案 0 :(得分:0)

此更改应该起作用,具体取决于您的代码示例:

<tr>
  <th>Voucher No</th>
  <th>Voucher_Category_By_Credit</th>
  <th>Voucher_Category_By_Debit</th>
</tr>

      <?php foreach($modResult as $voucher):?>
       <tr>
         <td><?php echo $voucher['voucher_no'];?></td>
         <td><?php echo $voucher['voucher_category_type']=='credit' ? $voucher['voucher_category'] : '';?></td>
         <td><?php echo $voucher['voucher_category_type']=='debit' ? $voucher['voucher_category'] : '';?></td>
        </tr>
       <?php endforeach; ?>