问题描述:

I'm trying to update a column of the table by batches of 200. For example, for the column 'weekday', the first 200 would get value '1', the second 200 would get value '2' and so on.

So far I've managed to get:

 $record=$this->db->get('user');

$i=0;

foreach ($record->result() as $row):

$data=array

(

'weekday'=>1,

);

$this->db->update('user', $data);

$i++;

if($i==200){

break;

}

endforeach;

echo '100 done';

foreach ($record->result() as $row):

$data=array

(

'weekday'=>2,

);

$this->db->update('user', $data);

$i++;

if($i==400){

break;

}

endforeach;

echo '200 done';

die;

But after the code is run, all rows' column 'weekday' values are updated to '2'. I'm not sure what happened. Thanks!

网友答案:

You dont need to break and go out, just put an if inside the for loop

$record=$this->db->get('user');

        $i=0;
        foreach ($record->result() as $row):

           if($i>=200){
               $data=array
                (
                    'weekday'=>2,
                );
            }
            else
           {
                $data=array
                (
                    'weekday'=>1,
                );

          }

          $this->db->update('user', $data);
          $i++;

        endforeach;
相关阅读:
Top