如何将最后一个插入ID数据发送到另一个函数?

时间:2019-05-31 07:17:49

标签: php codeigniter variables last-insert-id

当我使用var_dumb()测试$ ids var包含值,但是在提交$ ids var后不包含任何值时,我想将我的最后一个插入id从一个表传递到另一个函数。 有人可以告诉我如何在这段代码中做到这一点, 谢谢

这是我的控制器:

function save()
{
    $judul = $this->input->post('judul');
    $isi = $this->input->post('isi');
    $kategori = implode(',', $this->input->post('kategori'));
    $c_date=date("Y-m-d H:i:s");

    $data = array(
        'judul' => $judul,
        'isi' => $isi,
        'kategori' => $kategori,
        'publish' => $c_date
    );

    $ids = $this->text_editor_model->simpan($data);
    redirect('text_editor');

    $last = $this->upload_image($ids);
}

function upload_image($last)
{
    $config['upload_path'] = './berkas/news/';
    $config['allowed_types'] = 'jpg|png|jpeg';
    $config['max_size'] = 0;

    $this->load->library('upload', $config);

    var_dump($last);

    if ( !$this->upload->do_upload('file')) {
        $this->output->set_header('HTTP/1.0 500 Server Error');
        exit;
    } else {
        $file = $this->upload->data();
        $this->output
            ->set_content_type('application/json', 'utf-8')
            ->set_output(json_encode(['location' => base_url().'/berkas/news/'.$file['file_name']]))
            ->_display();
        $count = count($file['file_name']);
            for ($i=0; $i < $count ; $i++) {
                $data2[$i]['id_berita'] = $last;
                $data2[$i]['img_name'] =  $file['file_name'];
            }
            $this->text_editor_model->insert_img($data2);
            exit;
    }
}

我的辛潘模型:

function simpan($data)
{
    $this->db->insert('db_news',$data);

    $id_berita = $this->db->insert_id();
    return $id_berita;
}

1 个答案:

答案 0 :(得分:0)

如果redirect('text_editor');重定向到另一个页面?那么$ last = $this->upload_image($ids);将不会执行,因此您需要先调用上传然后重定向

$last = $this->upload_image($ids);
redirect('text_editor');