简体   繁体   中英

Not able to display data according to id in codeigniter

I have a list of clients and on click of each client i wish to display details of that particular client only, for this i used the following code.

The view of client list is something like this

id  fullname
1     A
2     B
3     C

When i click on A i get redirected to other page whose url is http://localhost/projname/admin/client/1

When i click on B i get redirected to other page whose url is http://localhost/projname/admin/client/2

And so on.... the id keeps on changing depending on the click

But the issue is that when i try to display the data in client_detail_view.php page i get the data of first id only ie id=1

Structure of codeigniter is

controllers
    -admin.php
model
    -admin_model.php
views
    -admin  
        -client_view.php
        -client_detail_view.php

Code for admin.php

public function client($clientid)
        {
            $data['client_data'] = $this->admin_model->get_client($clientid);
            $this->load->view('admin/client_detail_view',$data);
        }

Code for admin_model.php

public function get_client($id)
            {
                $query = $this->db->get('users');
                $this->db->where('id', $id);
                return $query->row();
            }

Code for client_view.php

<?php foreach($client as $perclient): ?>
    <tr>
        <?php echo "<td><a href='". base_url() ."admin/client/".$perclient->id."'>" . $perclient->fullname . "</a></td>"; ?>
        <td><?php echo $perclient->email; ?></td>
        <td><?php echo $perclient->contactno; ?></td>
    </tr> 
<?php endforeach; ?> 

Code for client_detail_view.php

<?php echo $client_data->fullname; ?>

Seems like query is run beofre setting where so it is rightly returning the first record only. Set where condition beofre query

You can change in model function :

admin_model.php

public function get_client($id)
{
     $this->db->where('id' => $id);
     $query = $this->db->get('users');
     return $query->row();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM