0

I am having dictionary as

array(
  'ID1' => 1 ,
  'ID2' => 2,
  'Status' => 'Done'
)

While I have only 2 columns in my table, ID and Status. I want to update two rows having IDs 1, and 2, using only one time ($this->db->update). Is it possible or should I have to make custom query. I want to do like ($this->db->update(where ID1 == 1 && ID2 == 2)).

3 Answers 3

1

Either

// Get your status 
$item = array( 'Status' => $response['Status'] );

// unset status
unset($response['Status']);

// I assume except status rest all values are ids so
// array_values gives id
$this->db->where_in('id', array_values($response));

// Update table
$this->db->update('table', $item );

OR

You have to modify your array

    $data = array(
       array(
         'id' => 1 ,
         'Status' => 'Done'
       ),
       array(
         'id' => 2 ,
         'Status' => 'Done'
       )
    );

and then

    $this->db->update_batch('table_name', $data, 'id');

Like this you can modify

( for comment : Dear I cant do so, because I am getting response from some service. And I am not able to edit this response. )

[akshay@localhost tmp]$ cat test.php
<?php
$response = array(
  'ID1' => 1 ,
  'ID2' => 2,
  'Status' => 'Done'
);

$data = array(); 
$item = array( 'Status' => $response['Status'] );
unset($response['Status']);

foreach($response as $new){
        $item['id'] = $new;
        $data[] = $item; 
}
print_r($data);

// Here you update 
// $this->db->update_batch('table_name', $data, 'id');
?>

Output

[akshay@localhost tmp]$ php test.php
Array
(
    [0] => Array
        (
            [Status] => Done
            [id] => 1
        )

    [1] => Array
        (
            [Status] => Done
            [id] => 2
        )

)
Sign up to request clarification or add additional context in comments.

Comments

0
//this will update the values where ids equal any value in the second were_in parameter

//data is array of values to update 
$data = [
   'Status'=>'some status',
];

//array of ids to update
$ids = [1,2];

$this->db->where_in('id', $ids)->update('table_name', $data);

Comments

0

It shouldn't be the responsibility of the model to validate, sanitize or otherwise prepare query data. Short of explaining how to do that in a library, perform the necessary processes before building the query.

public function batchUpdateStatus(array $payload): int
{
    if (empty($payload['status'])) {
        return 0;
    }
    $ids = array_filter(
        $payload,
        fn($k) => str_starts_with($k, 'ID'),
        ARRAY_FILTER_USE_KEY
    );
    if (!$ids) {
        return 0;
    }
    $bool = $this->db
        ->where_in('ID', $ids)
        ->update('your_table', ['Status' => $payload['Status']);

    return $bool ? $this->db->affected_rows() : 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.