Reputation: 6656
I have a two radio buttons on my form which is named as "status"and the values are 1 and 0. So when I submit the form and checked one of the radio buttons it's stored on the database. The problem is when I retrieve the data from database included the status column name, I wanted it to be set as "active" or "inactive".
View:
<table class="table table-condensed">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
<th>Status</th>
<th>Date Registered</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach($users as $key => $value) {?>
<tr>
<td><?php echo $value['firstname']; ?></td>
<td><?php echo $value['lastname']; ?></td>
<td><?php echo $value['email']; ?></td>
<td><?php echo $value['status']; ?></td>
<td><?php echo $value['datereg']; ?></td>
<td>
<a href="<?php echo site_url('main/showUser/'.$value['id']); ?>">
<i class="material-icons" title="Click to update">mode_edit</i></a> |
<a href="" class="remove"><i class="material-icons" title="Click to remove">
delete</i></a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
Controller:
public function usersPage() {
$data['users'] = $this->user->getUsers();
$this->load->view('user_page' , $data);
}
Model:
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;
}
Upvotes: 1
Views: 208
Reputation: 10548
<?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>
Upvotes: 2
Reputation: 56
easiest way, put this in your Model:
'status' => $value->status ? 'active' : 'inactive'
Upvotes: 3