Update Object value from Array of Objects Codeigniter

时间:2016-12-02 04:47:01

标签: php arrays codeigniter foreach

I have a query result in values $some = $query->result() , now $some[0] should be about containing following properties:

  • name
  • type(A/B)
  • status(0/1)

Now status of all objects contain null so i want to set 0 if type is A , and 1 if type is B, hence come to simple looping of Array of Objects as well as modifying them while looping , How to do this?

Edit:

Code and table details are given below

Table `test`:

Name| Type| status
Ali | A   | NULL
Zeeshan| A | NULL
Waqar| B | NULL


$query = $this->db->query("SELECT test.name as name,test.type as type , test.status as status FROM test");

$some = $query->result();

1 个答案:

答案 0 :(得分:0)

您应该能够使用UPDATE语句。由于您没有指定正在使用的SQL版本,因此我可以为几个常见数据库版本列出UPDATE文档的一些链接:

您应该可以使用CASE statement有条件地设置状态的值:

UPDATE test 
SET status = CASE type WHEN 'A' THEN 0 ELSE 1 END

您可以在此sqlfiddle上看到这一点。