循环代码点火器中的多个插入 [英] Multiple inserts in a loop codeigniter

查看:51
本文介绍了循环代码点火器中的多个插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,在我的数据库中,我有2个表.一个名为父亲,其中 Father_id PK ,并且 Father_name 为一行.然后,我将第二张表作为 Children (儿童),其中 Child_id 作为 PK Father_id 作为 FK >,并连续 Child_name .

Let's say in my DB i have 2 tables. One is named Father with Father_id as PK and Father_name as a row. Then i have the second table as Children with Child_id as PK, Father_id as FK, and Child_name as a row.

孩子与父亲之间的关系将是多对一的.很多孩子给一个父亲.

Children would have a relationship with Father as Many to One. Many Children to One Father.

因此,在我的控制器中,数据看起来像这样.

So let's say the data looks like this in my controller.

Array([Father_id] => 1 [Children] => Array([0] => John [1] => Peter [2] => Michael))

我将上面的数据传递给我的模型以进行插入,但是我必须以一种可以将其发送到数据库中进行插入的方式对其进行重组.

I pass the data above to my model to insert right but i have to restructure it in a way that i can just send it to my database to insert.

foreach($data as $row){
    for($temp = 0; $temp < count($data['Child_name']); $temp++){
        $query[$temp] = array('Father_id' => $data['Father_id'], 'child_name' => $row[$temp]);
    }
}

所以现在看起来像这样,

So now it looks like this,

Array([0]=>Array([Father_id] => 1 [Child_name] => John)[1]=>Array([Father_id] => 1 [Child_name] => Peter)[2]=>Array([Father_id] => 1 [Child_name] => Michael))

我知道如何插入它,我只需要执行另一个foreach循环,或者我可以删除 $ query [$ temp] 中的 $ temp 而是在该循环中插入一个插入查询.

I understand how to insert this, i'll just have to do another foreach loop or i can just remove the $temp in $query[$temp] and instead put an insert query inside that loop.

我的问题是,除了检查数据库之外,我如何知道每个插入是否成功,我希望有一个return语句,例如仅插入一个插入语句,如果成功则返回1.

My problem is that how can i know that each insert is successful, other than checking the database, i would like to have a return statement like if its only a single insert then if its successful it returns 1.

还有没有更好的方法可以进行多次插入?

Also is there any better way to do multiple inserts?

更新:

经过一些必要的休息后,我回来进行了一些更改.

After some needed break i came back and made some changes.

现在这是我的模型函数:

This is now my model function:

        public function insert_children($data){
        foreach($data['child_name'] as $row){
            $query = array('father_id' => $data['father_id'], 'child_name' => $row);
            $result = $this->db->insert('children',$query);
        }
  //return something here
}

必须将某些东西还给控制器,对吗?例如,如果插入是否成功,我想返回.那么,在codeigniter中是否已经有此功能?还是我必须让自己成为自己.

It is essential to return something back to the controller right? for example, i want to return if the inserts were successful or not. So is there any already made function in codeigniter for this? or do i have to make myself.

例如,每次插入成功后,我都可以制作一个 $ temp 变量以包含一个增量值,并将其在循环结束时与 count($ data['children'])

for example, i can just make a $temp variable to contain an incremental value each time an insert is successful and compare it at the end of the loop to the count($data['children'])

因此,如果有3个子代,则$ temp的值应为3或2,具体取决于声明,如果我将其指定为1,例如 $ temp = 1;

So if there are 3 children the value of $temp should be 3 or 2 depending on declaration if i specify it with 1 like $temp = 1;

推荐答案

您可以在每个循环项上使用 affected_rows()函数来检查是否插入了数据,并且仅返回有关未插入的数据:

You can use affected_rows() function on each loop item to check whether data is inserted or not and return error message only for the data that is not inserted :

foreach($data as $row) {
    for($temp = 0; $temp < count($data['Child_name']); $temp++){
        // note that I remove [$temp] below
        $query = array('Father_id' => $data['Father_id'], 'child_name' => $row[$temp]);
        $this->db->query($query);
        if($this->db->affected_rows() == 0) // data not inserted, return error message
        {
          $error[] = $row[$temp];
        }
    }
}

// shows error if one or more child name is not inserted
if (isset($error)) {
    foreach ($error as $child_name) {
        echo $child_name . ' is not inserted';
    }
}

这篇关于循环代码点火器中的多个插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆