3

I don't understand why this is not putting the database values on the page. Can you please tell me what I am doing wrong with this code? I'm a novice at CodeIgniter and this is the first time I'm trying to pass an array from model to controller to view.

Controller Code:

public function showuser(){
        $id = $this->uri->segment(3);
        $this->load->model('user_model');
        $data['user']= $this->user_model->view_user($id);       
        $this->load->view('include/header');
        $this->load->view('user_view',$data); 
        $this->load->view('include/footer');
}

Model Code:

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

View Code:

<table width="300" border="0" cellpadding="2">
  <tr>
    <td width="143">First Name</td>
    <td width="143"><?php echo $data['user']->firstname; ?></td>
  </tr>
  <tr>
    <td>Last Name</td>
    <td><?php echo $data->lastname; ?></td>
  </tr>
  <tr>
    <td>Company</td>
    <td><?php echo $data->company; ?></td>
  </tr>
  <tr>
    <td>Email</td>
    <td><?php echo $data->email; ?></td>
  </tr>
  <tr>
    <td>Phone</td>
    <td><?php echo $data->phone; ?></td>
  </tr>
  <tr>
    <td><p>Fax</p></td>
    <td><?php echo $data->fax; ?></td>
  </tr>
  <tr>
    <td>Address 1</td>
    <td><?php echo $data->address1; ?></td>
  </tr>
  <tr>
    <td><p>Address 2</p></td>
    <td><?php echo $data->address2; ?></td>
  </tr>
  <tr>
    <td>Address 3</td>
    <td><?php echo $data->address3; ?></td>
  </tr>
  <tr>
    <td>City</td>
    <td><?php echo $data->city; ?></td>
  </tr>
  <tr>
    <td>State/Region/Province</td>
    <td><?php echo $data->state; ?></td>
  </tr>
  <tr>
    <td>Postal Code</td>
    <td><?php echo $data->zipcode; ?></td>
  </tr>
  <tr>
    <td>Username</td>
    <td><?php echo $data->username; ?></td>
  </tr>
  <tr>
    <td>Account Type</td>
    <td><?php echo $data->usertype; ?></td>
  </tr>
</table>

2 Answers 2

3

You don't need to use variable $data inside your view. Just directly print variables by $data array keys, for example, $user->firstname etc. More about views: http://ellislab.com/codeigniter/user-guide/general/views.html

Also, I think you should have to return $query->row(), not $query->result() inside your model, because it is getting data just about one user.

Sign up to request clarification or add additional context in comments.

Comments

0

Another way is use foreach($user as $u) and print like $u['firstname'] etc in php tag.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.