我有一個 codeigniter 購物車網站,用戶可以在購物車中添加多個產品然后結帳,當用戶在結帳中單擊確認按鈕時,應將用戶詳細資訊和產品詳細資訊添加到資料庫中,我執行了以下代碼:
public function checkout() {
$data['items'] = array_values(unserialize($this->session->userdata('cart')));
$data['total'] = $this->total();
foreach ($data['items'] as $item){
$itemname=$item['name'];
$productid=$item['id'];
}
if(isset($_POST['confirm']))
{
$name=$this->input->post('name');
$phone=$this->input->post('phone');
$email=$this->input->post('email');
$address=$this->input->post('address');
$productname=$itemname;
$total=$this->input->post('total');
$productid=$productid;
$this->product->addorder($name,$phone,$email,$address,$productname,$total,$productid);
}
$this->load->view('checkout', $data);
}
所有的細節都被添加到資料庫中,但是有一個問題,你可以在 foreach 回圈中看到的 itemname 和 productid,即使購物車中有多個產品,只有第一個產品名稱和 id 被存盤在資料庫,我想將購物車中的所有商品名稱和 ID 保存到資料庫中,誰能告訴我這里有什么問題,在此先感謝
uj5u.com熱心網友回復:
你的回圈只創建一個資料,如果有多個資料,把你的插入查詢放在回圈代碼中,
嘗試這個
if(isset($_POST['confirm']))
{
foreach ($data['items'] as $item){
$itemname=$item['name'];
$productid=$item['id'];
$name=$this->input->post('name');
$phone=$this->input->post('phone');
$email=$this->input->post('email');
$address=$this->input->post('address');
$productname=$itemname;
$total=$this->input->post('total');
$productid=$productid;
$this->product->addorder($name,$phone,$email,$address,$productname,$total,$productid);
}
}
uj5u.com熱心網友回復:
$itemname = array();
$productid = array();
foreach ($data['items'] as $item){
array_push($itemname, $item['name']);
array_push($productid, $item['id']);
}
if(isset($_POST['confirm']))
{
$productname = json_encode($itemname);
$productid = json_encode($productid);
$this->product->addorder($name,$phone,$email,$address,$productname,$total,$productid);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/315054.html
