javascript - 无法在codeigniter中的foreach中获取数据

标签 javascript php codeigniter

这是我的 html 代码,我是 codeigniter 的新手,我不知道我在代码中犯了什么错误。这是一个从表中获取记录的简单查询。请帮助我。

<?php foreach($records as $r):?>
                <tr>
                    <td><?=$r['id']?></td>
                    <td><?=$r['name']?></td>
               </tr>
    <?php endforeach;?>

我的 Controller 代码

   public function index(){
            /*$this->load->view('student/student_view');*/
            $data['records']=$this->student_model->getAll();
            $data['student']=0;
            $this->load->view('student/student_view',$data);
}

和我的模态代码

function getAll(){
        $query = $this->db->get('stud');    
        return $query->result();
    }

最佳答案

您正在从模态返回一个像数据这样的对象,并且您正在以数组的形式获取该数据,因此您需要将任一模态更改为

function getAll(){
        $query = $this->db->get('stud');    
        return $query->result_array();
    }

使用相同的 View ,现在 html 代码没有变化。 别的 为模态保留相同的代码

function getAll(){
        $query = $this->db->get('stud');    
        return $query->result();
    }

更改 html View 代码

<?php foreach($records as $r):?>
                <tr>
                    <td><?=$r->id?></td>
                    <td><?=$r->name?></td>
               </tr>

    <?php endforeach;?>

通常我们使用 -> 来表示对象,使用 [] 来获取数组记录。希望这会起作用。祝你好运。

关于javascript - 无法在codeigniter中的foreach中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25914449/

相关文章:

php - 分页当前链接不突出显示

带身份验证的 Codeigniter 文件上传

javascript - 代码审查 - if else 函数

javascript - 如何从 JavaScript 中的 promise 链中的函数访问 'this'?

javascript - AngularJS,如何提交选项值?

javascript - 如何将 Ajax 响应转换为字符串以与 JS 字符串进行比较

PHP - 在 URL 中用 https 替换 http

javascript - cal-heatmap - 如何根据 rect .q1{} 颜色更改文本颜色?

php - 是否存在将 Unicode 文本大写的可靠方法?

php - CodeIgniter - 如何在整个程序中使用对象变量