【问题标题】:CodeIgniter - Get next record from an arrayCodeIgniter - 从数组中获取下一条记录
【发布时间】:2014-07-16 09:08:24
【问题描述】:

我将所有数据从表中获取到数组中:

$records = $this->Misc->getAll('table');

接下来我做了一个循环

foreach ($records as $key => $record) {     
    // here i want to update a column in the fist
    // row with a value from the second row
    $this->Misc->update($record->id; $value_of_a_column_from_the_next_row);
}

我该怎么做? 谢谢

【问题讨论】:

  • 这很不寻常,你到底想做什么?我可以给你一个“解决方案”,但我怀疑它是否真的能解决你的问题。
  • 您只想更新第一条记录还是要从所有条记录中转移值?更多上下文会有所帮助。
  • 第一行 'a1','a2','a3','null ','a4' ..... 第二行 'b1','b2','b3','X' ,'b4' .....我想将X添加到空位置。

标签: php mysql arrays codeigniter


【解决方案1】:

你可以像这样使用你的 foreach 循环:

foreach ($records as $key => $record) {
    if (isset($records[$key-1]) {
        $updateID = $records[$key-1];
        $this->Misc->update($updateID, $record['field'] );
    }
}

或者反过来:

foreach ($records as $key => $record) {
    if (isset($records[$key+1]) {
        $updateID = $records[$key+1];
        $this->Misc->update($updateID, $record['field'] );
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 1970-01-01
    • 2014-11-08
    • 2012-07-05
    相关资源
    最近更新 更多