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 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思