从数据库中检索时更改单选按钮值

时间:2015-11-30 14:56:23

标签: php mysql codeigniter

我的表单上有两个单选按钮,名为“status”,值为1和0.因此,当我提交表单并选中其中一个单选按钮时,它存储在数据库中。问题是当我从包含状态列名称的数据库中检索数据时,我希望将其设置为“活动”或“非活动”。

查看:

  public function usersPage() {
    $data['users'] = $this->user->getUsers();
    $this->load->view('user_page' , $data);
  }

控制器:

public function getUsers() {
    $this->db->select('id, email, first_name, last_name, date_registered, status');
    $this->db->from('auth_users');
    $query = $this->db->get();

    $result = $query->result();

      foreach ($result as $key => $value) {
    $data[$key] = array(
      'id'  => $value->id,
      'firstname' => $value->first_name,
      'lastname' => $value->last_name,
      'datereg' => $value->date_registered,
      'email' => $value->email,
        'status' => $value->status
      );
    }
    return $data;
  } 

型号:

import Data.List

unique x = union x x

2 个答案:

答案 0 :(得分:3)

最简单的方法,将其放入您的模型中:

'status' => $value->status ? 'active' : 'inactive'

答案 1 :(得分:2)

<?php foreach($users as $key => $value) 
{
    // Write Here Like This.
    $status=$value['status'] == 1 ?'active':'inactive';
?>
  <tr>
    <td><?php echo $value['firstname']; ?></td>
    <td><?php echo $value['lastname']; ?></td>
    <td><?php echo $value['email']; ?></td>
    <td><?php echo $status; ?></td>
    <td><?php echo $value['datereg']; ?></td>
    <td>