-1

I have a problem with Codeigniter. I am unable to display data in view file. Given below is the code of my model, controller and view file. I have also attached a screenshot of the error for better clarity.

Model

public function get_projects ($userid = '')
 {
   if (empty($userid)){
     return FALSE;
   }
        $this->db->select('*');
        $this->db->from('projects');
        $this->db->where('userid', $userid);
        $query = $this->db->get();

   return $query->row_array();                 
 }

Controller

$uid = $this->session->userdata('user_id');

        $data['details']= $this->projects->get_projects($uid);
        $this->load->view('design/header', $data);
        $this->load->view('design/nav', $data);
        $this->load->view('content/dashboard', $data);
        $this->load->view('design/footer', $data);

View

<div class="col-6">
            <?php foreach ($details as $row){ echo $row->id; } ?>
        </div>

Error

enter image description here

8
  • Have you ever tried to check if it is an associative array instead? The return $query->row_array(); Seeps that returns the data as an associative array. Commented Jun 20, 2019 at 18:28
  • And did it work or not? Is has not worked then update your question with your trial ;) . Also update the question showing the result of var_dump($row); furthermore in theese situations you can use a tool named xdebug as well. Commented Jun 20, 2019 at 18:40
  • it did not work. I also tried changing my model file to if($query->num_rows() == 1){ return $query->row(); }else{ return $query->result(); } Commented Jun 20, 2019 at 18:42
  • no luck. I think I have a really stupid mistake Commented Jun 20, 2019 at 18:43
  • On your view just place var_dump($row) on the first line of it from that figure out what type of variable is. (After you debugged it just remove it). About var_dump() look on documentation. Commented Jun 20, 2019 at 18:48

3 Answers 3

1

The problem here is the return of model function: return $query->row_array(); that returns only 1 array (or return $query->row(); that also returns only 1 object). While you are iterating it in view.

Not sure about your purpose, but you can fix it by or:

  • Do not use iteration in view, just use the variable $details->id
  • Change your return $query->row_array(); to return $query->result();
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah I found the problem and now understand it. Thanks a lot.
1

First off, it sounds like you want an object as you are using object notation $row->id and not $row['id']. This means you need to use row(). This also means since you will have 1 result, you don't need a foreach.

Model:

public function get_project($userid) {
   $q = $this->db->get_where('projects', array('userid', $userid));
   if ($q->num_rows() !== 1) {
       return false;
   }
   return $q->row();                 
 }

Controller:

$uid = $this->session->userdata('user_id');
if (empty($uid)) {
    show_error('user id not set'); // we probably shouldn't have gotten this far
}
$project = $this->projects->get_project($uid);
if ($project === FALSE) {
    show_error("no rows in project table for user with id: $uid");
}
$data['details'] = $project;
$this->load->view('design/header', $data);
$this->load->view('design/nav', $data);
$this->load->view('content/dashboard', $data);
$this->load->view('design/footer', $data);

View:

<p><?php echo $details->id; ?></p>
<p><?php echo $details->someothervar; ?></p>

While this code may not resolve your problem (I am guessing that there are either no projects for a user with that id or that the session uid is incorrectly set) it will help you debug things and it is good practice to always check for num_rows() and that your variables are outputting what you expect they are. I tend to keep all of these checks in the controller.

1 Comment

thanks Alex for your reply. I came to know some important things through this.
0

In your view page change $row->id; to $row['id'] it will generate result

<div class="col-6">
            <?php foreach ($details as $row){ echo $row['id']; } ?>
 </div>

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.