user7785761
user7785761

Reputation:

Undefined variable,index error fetching data codeigniter

Im fetching data from a table to another table that will be shown in my dashboard page for a user after loggin in.

But there is a problem with the indexes, i got this error:

enter image description here

Here is the line error:

enter image description here

Here is my code:

My view file ("usuario"):

                    <thead>
                    <th>id</th>
                    <th>User</th>
                    <th>Subject</th>
                    <th>Grade</th>
                    <th>Date</th>
                </thead>


<tbody>
    <?php

    if (count($records) > 0 && $records != false) {
        foreach($records as $record) {

            echo "<tr>
                      <td>".$record['id']."</td>
                      <td>".$record['User']."</td>
                      <td>".$record['name']."</td>
                      <td>".$record['grade']."</td>
                      <td>".$record['date']."</td>
                  </tr>";
        }

       }
    ?>

</tbody>

</body>
</html>

My controller file ("login"):

    public function home(){

    $data['record']=$this->m_login->getDetails();
    $this->load->view('usuario',$data);
}

My model file ("m_login"):

        public function getDetails()
        {
            $st=$this->db->SELECT('cursadas.*, usuarios.name as usuarios, materias.name as materias_name')->from('cursadas')
                ->join('usuarios','usuarios.id=cursadas.user_id')
                ->join('materias','materias.id=cursadas.subject_id')
                ->WHERE('cursadas.user_id=',$this->session->userdata['id'])
                ->get()->result_array();
            return $st[0]; 
        }

Upvotes: 0

Views: 332

Answers (3)

user4419336
user4419336

Reputation:

You have the variable $records on view but not on controller

Change

$data['record'] = $this->m_login->getDetails();

To

// add this array() just in case no results found 

$data['records'] = array(); 
$data['records'] = $this->m_login->getDetails();

$this->load->view('usuario', $data);

Another way is on controller

$results = $this->m_login->getDetails();

$data['records'] = array(); 

if ($results) {
   foreach ($results as $result) {
      $data['records'][] = array(
         'id' => $result['id'],
         'User' => $result['User'],
         'name' => $result['name'],
         'grade' => $result['grade'],
         'date' => $result['date']
      );
   }
}

$this->load->view('usuario',$data);

View

<?php if ($records) {?>
<?php foreach($records as $record) {?>
<tr>
<td><?php echo $record['id'];?></td>
<td><?php echo $record['User'];?></td>
<td><?php echo $record['name'];?></td>
<td><?php echo $record['grade'];?></td>
<td><?php echo $record['date'];?></td>
</tr>
<?php } ?>
<?php } else { ?>
<tr> 
<td>No Results Found</td>
</tr>
<?php } ?>

Upvotes: 1

Aman Attari
Aman Attari

Reputation: 191

result_array() return returns result with array so you need to it like this -

if (count($record) > 0 && $record != false) {
    foreach($record as $rec){
        echo "<tr>
                  <td>".$rec['id']."</td>
                  <td>".$rec['User']."</td>
                  <td>".$rec['name']."</td>
                  <td>".$rec['grade']."</td>
                  <td>".$rec['date']."</td>
              </tr>";
    }
   }

Please check this from above code and comment if you have any problem

Upvotes: 0

rango
rango

Reputation: 619

Wrong array index in your controller.

Change this

$data['record']=$this->m_login->getDetails();

to

 $data['records']=$this->m_login->getDetails();

Upvotes: 0

Related Questions