大家好我只需要一点帮助就可以使用循环在我的表中插入信息。 我不知道我的错误在哪里。
这是我的代码:在我的INSERT模型中
$poid = $this->input->post('po_id');
$cash_delivery = $this->input->post('cash_delivery');
$cash_check = $this->input->post('cash_check');
$bank_transfer = $this->input->post('bank_transfer');
$itemname = $this->input->post('item'); // array of item name
$quantity = $this->input->post('qty'); // array of quantity
$price = $this->input->post('price'); //array of price
$total = $this->input->post('total'); //array of total
//CHECK IF TRANSACTION IS CHECKED
$val_delivery = NULL;
$val_check = NULL;
$val_transfer = NULL;
if(isset($_POST['cash_delivery'])){
$val_delivery = 'Y';
}else{
$val_delivery = 'N';
}
if(isset($_POST['cash_check'])){
$val_check = 'Y';
}else{
$val_check = 'N';
}
if(isset($_POST['bank_transfer'])){
$val_transfer = 'Y';
}else{
$val_transfer = 'N';
}
$filtername = array_filter($itemname);
$filterquantity = array_filter($quantity);
$filterprice = array_filter($price);
$filtertotal = array_filter($total);
//INSERT ORDERS
for($x = 0; $x < sizeof($filtername); $x++){
$orders = array(
'poid' => null,
'order_id' => $poid,
'item_desc' => $filtername[$x],
'item_qty' => $filterquantity[$x],
'item_price' => $filtertotal[$x],
'total' => $filtertotal[$x],
'cash_on_delivery' => $val_delivery,
'is_check' => $val_check,
'bank_transfer' => $val_transfer,
'transaction_date' => $dateorder
);
$this->db->insert('po_order',$orders); //Only first item (index[0]) is added
echo "<pre>";
print_r($orders); //This will print my array values 'NO ERROR HERE'
echo "<hr />";
}
My database table design:
mysql> desc po_order;
+------------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+---------------------+------+-----+---------+----------------+
| poid | int(10) unsigned | NO | PRI | NULL | auto_increment |
| order_id | varchar(50) | NO | UNI | 0 | |
| item_desc | varchar(50) | NO | | NULL | |
| item_qty | int(10) unsigned | NO | | NULL | |
| item_price | float(7,2) unsigned | NO | | NULL | |
| total | float(7,2) unsigned | NO | | NULL | |
| cash_on_delivery | enum('Y','N') | NO | | NULL | |
| is_check | enum('Y','N') | NO | | NULL | |
| bank_transfer | enum('Y','N') | NO | | NULL | |
| transaction_date | datetime | NO | | NULL | |
+------------------+---------------------+------+-----+---------+----------------+
我的问题是只添加了循环中的第一项。我无法从循环中获取下一个项目。
答案 0 :(得分:1)
恕我直言,你没有从$ itemname = $ this-&gt; input-&gt; post('item')获得正确的价值; //数组应该返回吗?
试着看看它是否确实如此。
以及你的for($ x = 0; $ x&lt; sizeof($ filtername); $ x ++){
sizeof($ filtername)会给你什么?
尝试
die('for loop will iterate "'.sizeof($filtername).'" times');