未定义的变量,索引错误获取数据codeigniter

时间:2017-04-08 16:48:04

标签: php codeigniter fetch

我从表中获取数据到另一个表,该表将在登录后显示在用户的仪表板页面中。

但是索引存在问题,我收到了这个错误:

enter image description here

这是行错误:

enter image description here

以下是我的代码:

我的观点文件(" 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>

我的控制器文件(&#34;登录&#34;):

    public function home(){

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

我的模型文件(&#34; m_login&#34;):

        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 个答案:

答案 0 :(得分:1)

您在视图上有变量$records但在控制器上没有变量

更改

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

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

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

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

另一种方法是在控制器上

$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);

查看

<?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 } ?>

答案 1 :(得分:0)

控制器中的数组索引错误。

更改此

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

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

答案 2 :(得分:0)

result_array()返回返回带数组的结果,所以你需要像这样 -

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

如果您有任何问题,请从上面的代码中查看并注释