php - SEARCHING with some condition in codeigniter -
i have web project using codeigniter. have problem searching in project. have show multiple result searching page keyword. here model
function find_user($like){ $this->db->from('user'); $this->db->like('name', $like,'after'); $result =$this->db->get(); return $result->result_array(); } and in user table, include
id | name | place_code in user table , column place_code use show place user
here controller
function search(){ $query = $this->input->post('query_cari'); $find = $this->m_user->find_user($query); foreach ($find $key) { $code = $key['place_code']; if ($code == '1') { $place = 'secretray'; }elseif($code == '2'){ $place = 'ob'; }elseif($code == '3'){ $place ='manager'; } } $data['result'] = $find; $data['place'] = $place; $this->load->view('home/search',$data); } that's code controller, include logic position user in office. problem is, when result 1 result, place right. if more 1 result, place going wrong , show place result place last result in searching. want is, result shown own place.
here view
<?php foreach ($find $key: ?> <table> <thead> <tr> <th>id</th> <th>name</th> <th>place</th> </tr> </thead> <tbody> <tr> <td><?php echo $key['id'] ?></td> <td><?php echo $key['name'] ?></td> <td><?php echo $place ?></td> </tr> </tbody> </table> <?php endforeach ?>
it normal last place because logic not correct. first, have error in model. secondly, can improve model code:
function find_user($like) { $this->db->like('name', $like, 'after'); return $this->db->get('user')->result(); } now, in controller want change variable place according value of place_code. allowed add new key (or change existing one) in real time stdclass() follows:
foreach($find $i => $result) { // default, assume 'place_code' equal '1' $find[$i]->place = 'secretray'; if($result->place_code == '2') $find[$i]->place = 'ob'; else $find[$i]->place = 'manager'; } $data['find'] = $find; $this->load->view('home/search', $data); finally, in view:
<?php foreach ($find $result){ ?> <table> <thead> <tr> <th>id</th> <th>name</th> <th>place</th> </tr> </thead> <tbody> <tr> <td><?php echo $result->id; ?></td> <td><?php echo $result->name; ?></td> <td><?php echo $result->place; ?></td> </tr> </tbody> </table> <?php } ?>
Comments
Post a Comment