php - 我想在两个 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);
}
}
它甚至没有打印发布的结果..提前谢谢..:)
解决方案
如果您想将数据从 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?
}
}
推荐阅读
- node.js - How to exclude a field on save in node js
- amazon-web-services - Direct requests to a specific path based on the subdomain
- c# - Form display message when all items have been deleted from list
- c# - Error combining 'if' statements that null-checks and Pattern Matches
- r - finding each pattern ina set of sequences
- python - Numpy generate X random binary numbers with a define threshold in 1 line
- android - ArCore - 在会话上下文中创建锚点
- sql-server - 未使用表的索引碎片是否增加?
- prolog - Prolog:返回变量,检查变量是否属于某种类型?
- ios - Swift Sprite-Kit - 如何添加通知