phpcodeignitermodel-view-controller

Foreach loop is only saving the last iterated value


i have a web project using codeigniter. i have a problem with searching in my project. i have to show the multiple result from searching 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 , column place_code is use to show 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);
}

that's my code for controller, include a logic for get the position from user in office. but the problem is, when i get a result just 1 result, the place is right. but if i get more than 1 result, place is going wrong and just show the place for all result is the place from the last result in searching. what i want is, all result just shown their own place.

here 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 ?>

Solution

  • It is normal that you always get the last place because your logic is not correct. First, you have an error in your model. Secondly, you can improve your 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 according to the value of place_code. You are allowed to add a new key (or change an existing one) in real time to the 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 view:

     <?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 } ?>