如何在codeigniter中插入多个值数据

问题描述 投票:0回答:3

在我的选择选项中,我有两个数字的值示例value =“163, 162”

我有一个模型函数,可以在其中插入第一个值编号,即类别 ID 163

变量转储

array(2) { [0]=> string(3) "163" [1]=> string(4) " 162" }

问题:我需要添加

parent_id = " . $this->db->escape($category['parent_id']) . "
但不知道如何获取第二个值并将其设置为
parent_id = " .  $this->db->escape($category['parent_id']) . "
父ID例如162

模型功能

public function page_update($page_id, $data) {
    $this->db->query("DELETE FROM " . $this->db->dbprefix . "page_to_category WHERE page_id = '" . (int)$page_id . "'");

    if (isset($data['categories'])) {
        foreach ($data['categories'] as $category) {

            //$data_sample = explode(',', $category['category_id']);
            //var_dump($data_sample);
            //exit;

            $this->db->query("INSERT INTO " . $this->db->dbprefix . "page_to_category SET 
            page_id = '" . (int)$page_id . "', 
            category_id = " .  $this->db->escape($category['category_id']) . "
            ");
        }
    }
}

查看

<div class="form-group">
    <label class="col-sm-2">Category</label>
    <div class="col-sm-10">
        <select class="form-control" name="categories[1][category_id]">
            <option value="0">-- No Category Required --</option>
            <?php foreach ($categories as $category) {?>
            <?php if ($category['category_id'] == $category_id) {?>
            <option value="<?php echo $category['category_id'];?>, <?php echo $category['parent_id'];?>" selected="selected">
            <?php echo $category['name'];?></option>
            <?php } else { ?>
            <option value="<?php echo $category['category_id'];?>, <?php echo $category['parent_id'];?>"><?php echo $category['name'];?></option>
            <?php }?>
            <?php }?>
        </select>
    </div>
</div>
php codeigniter codeigniter-3
3个回答
2
投票

使用 Codeigniter 语法非常简单且更好,如果您可能想看一下,此代码与 @Arcanix 回答的相同,但使用 CI Active Record 语法,

public function page_update($page_id, $data)
{
    $this->db->delete('page_to_category', array('page_id' => (int)$page_id)); 

    if (isset($data['categories']))
    {
        foreach ($data['categories'] as $category)
        {
            $data_sample = array_map("trim",explode(',', $category['category_id']));

            $insert_data = array(
                            'page_id' => (int)$page_id,
                            'category_id' => $this->db->escape($data_sample[0])
                        );

            $this->db->insert('page_to_category', $insert_data); 
        }
    }
}

1
投票

答案是这样的:

public function page_update($page_id, $data) {
    $this->db->query("DELETE FROM " . $this->db->dbprefix . "page_to_category WHERE page_id = '" . (int)$page_id . "'");

    if (isset($data['categories'])) {
        foreach ($data['categories'] as $category) {

            $data_sample = array_map("trim",explode(',', $category['category_id']));
//Here $data_sample[0] will always be your category id and $data_sample[1] will be your parent id


            $this->db->query("INSERT INTO " . $this->db->dbprefix . "page_to_category SET 
            page_id = '" . (int)$page_id . "', 
            category_id = " .  $this->db->escape($data_sample[0]) . "
            ");
        }
    }
}

这里 $data_sample[0] 将始终是您的类别 id,$data_sample[1] 将是您的父级 id


0
投票

如果您想进行多次插入,您可以使用活动记录来完成。首先创建一个

array
要插入的数据,然后使用
insert_batch
方法

$insertdata = array(
   array(
      'email' => 'email' ,
      'name' => 'My Name' ,
      'date' => 'My date'
   ),
   array(
      'email' => 'some data' ,
      'name' => 'some data' ,
      'date' => 'some data '
   )
);
$this->db->insert('mytable', $insertdata); 

您可以从这里获取更多信息插入CodeIgniter中使用的方法

© www.soinside.com 2019 - 2024. All rights reserved.