douqiao6563 2014-06-23 17:48
浏览 58
已采纳

CodeIgniter MVC和循环通过MySQL表

I'm trying to create a iterate through a row in a MySQL table in one of my CodeIgniter projects, how could I "loop" through the table? Is it just a simple for loop like in other languages?

EDIT:

The answer is as followed:

$query = $this->db->get('mytable'); // select table "mytable" from database

foreach ($query->result() as $row) { // loop thru table and access each row's field 
                                     // by using $row->fieldname


}
  • 写回答

1条回答 默认 最新

  • duan_2000 2014-06-23 17:55
    关注

    Maybe something like this

    //$this->db->limit(10); // Optional if you want to limit, read about it
    $result = $this->db->get('server'); //return all rows
    
    foreach ($result as $row) {
        $row->status = 'inactive'; // change value of status attribute or whatever
        $this->db->update('server', $row)
    }
    

    Or maybe use $this->db->update_batch(); to update a stack of rows at one time.

    I encourage you to read the CI database class documentation too.

    Another suggestion, is to do all the business logic inside a model instead of a controller. But it's a matter of personal preference maybe.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题