0

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]; 
        }

3 Answers 3

1

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 } ?>
Sign up to request clarification or add additional context in comments.

Comments

0

Wrong array index in your controller.

Change this

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

to

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

1 Comment

Remove that && $records != false from if statement
0

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

8 Comments

Can you paste array of result means - you need to echo "<pre>"; print_r($recoord); echo "</pre>"; after foreach and before echo "</tr>
yes add ; after echo "</pre>"; and exit; then run the app
echo "<pre>"; print_r($records); echo "<pre>"; exit; paste this line before if (count($records) > 0 && $records != false) { and paste output
|

Your Answer

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