首页 > 解决方案 > 我想在两个 CodeIgniter 函数之间发送数据

问题描述

我必须将一个函数的值传递给另一个函数..

我写了这样的第一个函数。

function test1(){
        $product_details[] = array(
            'product_id' => '1',
            'count'      => '2'
        );
        $this->test2($product_details);
}    

我这样写了第二个函数。我必须保留这个 $_POST 必须的。

function test2(){
        $product_details = $_POST['product_details'];
        foreach($product_details as $row){
            $this->db->insert('table',$row);
        }
}

它甚至没有打印发布的结果..提前谢谢..:)

标签: phpcodeigniter

解决方案


如果您想将数据从 test1 传递到 test2,这在 test1 中看起来像

function test1()
{
    $product_details[] = array(
        'product_id' => '1',
        'count'      => '2'
    );
    $this->test2($product_details); # here you are trying to pass the $product_details to test2
}    

test2 的签名必须反映:

function test2(array $product_details) # this is the function signature
{
    $product_details = $_POST['product_details']; # this line would currently override your $product_details even if you passed them from test1
    $product_details[] = $_POST['product_details']; # maybe this is what you meant?
    foreach($product_details as $row){
        $this->db->insert('table',$row); # I imagine you tried echo $row or var_dump($row) here before?
    }
}

推荐阅读