php CODEIGNITER 未定义索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19442599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:28:04  来源:igfitidea点击:

CODEIGNITER undefined index

phpcodeigniter

提问by mabbs

:D got some problems here

:D 这里有一些问题

error code

错误代码

A PHP Error was encountered

遇到 PHP 错误

Severity: Notice

严重性:注意

Message: Undefined index: DESCRIPTION

消息:未定义索引:描述

Filename: views/dashboard_view.php

文件名:views/dashboard_view.php

Line Number: 13

行号:13

the controller:

控制器:

 function index()
 {
   if($this->session->userdata('logged_in'))
   {
     $session_data = $this->session->userdata('logged_in');
     $data['username'] = $session_data['USERNAME'];
     $data['companyid'] = $session_data['COMPANYID'];
     $data['category']=$this->main_model->get_category();
     $this->load->view('dashboard_view', $data);


   }
   else
   {
     //If no session, redirect to login page
     redirect('main', 'refresh');
   }
 }

model

模型

 function get_category(){
$this->db->select('*');
$this->db->from('view_category');
$category=$this->db->get();
return $category->result();
}

view

看法

<h2>Welcome <?php echo $username; ?>!</h2>
<?= form_hidden ($companyid); ?>
<br>
<a href="login/logout">Logout</a>
<? foreach($category ):?>
<tr>

    <td><?= $category['DESCRIPTION']; ?></td><-- this is line 13
</tr>

<? endforeach;?>

回答by Sushil Kumar Singh

Follow these steps :-

按着这些次序 :-

  • Check if your model is loaded correctly or not . Either load it manually before calling it or you can autoload your model in config/autoload file.

  • Before looping through your catogory in view file , try printing the array . I think there is no index named description in your category array .

  • Change :-

    $category->result(); 
    

    To :-

    $category->result_array();
    
  • Also :-

    <td><?= $category['DESCRIPTION']; ?></td>
    

    To :-

    <td><?php echo $category['DESCRIPTION']; ?></td>
    
  • 检查您的模型是否正确加载。在调用之前手动加载它,或者您可以在配置/自动加载文件中自动加载您的模型。

  • 在循环查看视图文件中的类别之前,请尝试打印数组。我认为您的类别数组中没有名为 description 的索引。

  • 改变 :-

    $category->result(); 
    

    到 :-

    $category->result_array();
    
  • 还 :-

    <td><?= $category['DESCRIPTION']; ?></td>
    

    到 :-

    <td><?php echo $category['DESCRIPTION']; ?></td>
    

Hope it helps you :)

希望对你有帮助:)

回答by Code L?ver

You foreach should be this:

你 foreach 应该是这样的:

<?php foreach($category as $rows): ?>
                      ^^^ add this

And then get the data by this:

然后通过这个获取数据:

<td><?php echo $rows['DESCRIPTION']; ?></td>

You can also check that what it is returning to get that just do print_r($rows);in the foreach loop.

您还可以检查它返回的内容以获取print_r($rows);在 foreach 循环中执行的操作。

Note: avoid to use the sort tag of PHP as many servers are not supporting to this.

注意:避免使用 PHP 的 sort 标签,因为很多服务器不支持这个。

回答by Cybermatatu

Have you tried loading the model before using it?

在使用模型之前,您是否尝试过加载模型?

Example: $this->load->model('main_model');

例子: $this->load->model('main_model');