SEARCH with some condition in codeigniter

I have a web project using codeigniter. I have a search problem in my project. i have to show multiple result on search page with some keyword. here is my 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 my user table, include

id | name | place_code

      

in the user table, the column is place_code

used to display the place from the user

here is my controller

    function search(){

    $query       = $this->input->post('query_cari');

    $find = $this->m_user->find_user($query);
    foreach ($find as $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);
}

      

what my code for the controller includes logic

to get the position from the user at the office. but the problem is that when I get the result only 1

, the result is place

right. but if I get more than one result, it place

goes wrong and just shows place

for the whole result place

from the last result when searching. I want all the results to just show their own place

.

here is my VIEW

 <?php foreach ($find as $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 ?>

      

+3


source to share


1 answer


It's ok that you always get the last place because your logic is wrong. First, you have a bug in your model. Second, you can improve the model code:

function find_user($like)
{
   $this->db->like('name', $like, 'after');

   return $this->db->get('user')->result();
}

      

Now in your controller, you want to change the variable place

to match the value place_code

. You can add a new key (or change an existing one) in real time stdClass()

as follows:



foreach($find as $i => $result)
{
   // By default, we assume the 'place_code' is equal to '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 your opinion:

 <?php foreach ($find as $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 } ?>

      

+2


source







All Articles