dpppic5186 2016-02-01 11:18
浏览 52
已采纳

将数组转换为String Codeigniter 3.0

I am trying to insert an element into my 'category' table and use the inserted element's id to insert another element into my 'subcategory' table.

This is my code in my controller

public function insertCategory(){
    $category = $this->input->post('category');
    $subcategory = $this->input->post('subcategory');

    $this->form_validation->set_rules('category', 'Category', 'required');
    $this->form_validation->set_rules('subcategory', 'Subcategory', 'required');

    if($this->form_validation->run() == FALSE){
        $this->load->view('ADMIN_ADDCategory');
    }
    else{
        $category_id = $this->admin_model->insertCategory($category);
        $this->admin_model->insertSubcategory($category_id, $subcategory);
    }
}

And this is my code in my model

function insertCategory($category){
    $data = array(
        'category' => $category,
        'status' => 1
    );
    $this->db->insert('category', $data);
    $this->db->select('id');
    $this->db->from('category');
    $this->db->where('category', $category);
    $this->db->limit(1);
    $query = $this->db->get();
    return $query->result();
}

function insertSubcategory($category_id, $subcategory){
    $data = array(
        'category_id' => $category_id,
        'subcategory' => $subcategory,
        'status' => 1
    );
    $this->db->insert('subcategory', $data);
}

However I am getting this error

enter image description here

I already tried using $category_id = (array) $this->admin_model->insertCategory($category); but it still doesn't work

How do I overcome this error? Thank you for the help.

  • 写回答

3条回答 默认 最新

  • duanbicheng3345 2016-02-01 11:40
    关注

    $this->admin_model->insertSubcategory($category_id, $subcategory);

    $category_id is an array , so , you must do it like this:

    $this->admin_model->insertSubcategory($category_id[0]->field, $subcategory);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用
  • ¥20 51单片机学习中的问题
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库