Save all items from the cart

0

I am looking for help for this problem that I have when I save my items that I add to the shopping cart, if I add several to the cart, it only saves me one and the price and the quantity keeps the total of everything I can do. I kept all the ids of the items and its the amount that corresponds to it and its price. I am new to this if you can explain to me in detail what I have to modify I would appreciate it.

$nombre = $this->input->post('nombre');
$apellido = $this->input->post('apellido');
$nueva_insercionCliente = $this->catalogo_model->insertarCliente($nombre,$apellido);
$idCliente = $this->db->insert_id();
$carrito = $this->cart->contents();

foreach ($carrito as $item) {
    $idProducto = $item['id'];

    if ($item['id'] == $idProducto) {
        $cantidad = 1 + $item['qty'];
        $total =$item['price'] * $cantidad;
    }
}

$nueva_insercionPedido=$this->catalogo_model->insertarPedido($idProducto,$idCliente,$cantidad,$total);
redirect(base_url("catalogo"),"refresh"); 
    
asked by Diego 09.06.2017 в 21:51
source

2 answers

1

What you should do is put the insert in foreach so that in each iteration the data contained in your shopping cart is inserted. Remaining your code as follows:

$nombre = $this->input->post('nombre');
$apellido = $this->input->post('apellido');
$nueva_insercionCliente = $this->catalogo_model->insertarCliente($nombre,$apellido);
$idCliente = $this->db->insert_id();

$carrito = $this->cart->contents();

foreach ($carrito as $item) {
    $idProducto = $item['id'];
    $cantidad = 1 + $item['qty'];
    $total =$item['price'] * $cantidad;
    $nueva_insercionPedido=$this->catalogo_model->insertarPedido($idProducto,$idCliente,$cantidad,$total);
}
redirect(base_url("catalogo"),"refresh");

Only saved the first because it is stored in each of your variables.

    
answered by 09.06.2017 / 22:00
source
0

Personally I think it is better to make an insert in the following way:

$query = "INSERT INTO xtabla (id,nombre,edad) VALUES ";
$x = 0;
$total = count($array);
foreach($array as $value){
  $query .= "(null,'".$value['nombre']."','".$value['edad']."')";
  $x++;
  if( $x < $total ){
    $query .= ",";
  }
}

$resultado = $conexion->query($query);
return $resultado;

This way you would make an INSERT of x amount of data in a single query and it would be much faster.

    
answered by 10.06.2017 в 19:04