Counting of items in the cart

Asked

Viewed 199 times

1

I’m making a cart in ajax, but I’m having trouble sending the products to the bank. I could use CSS in the tag li to indicate item numbering, but does not solve the problem for sending to bank.

HTML

<input name="produto_solicitado[][produto]" value="7845">

The part of produto_selecionado[] in each [] would be the item number (produto_selecionado[1],produto_selecionado[2]..). However I am unable to apply this operation when the product is added to the cart.

PHP

if (isset($_POST['produto_solicitado'])){
    $PedidoID= ($_POST['pedidoID']);
    $array = ($_POST['produto_solicitado']);
    $quant=count($array);
    // conexão
    $hostname="localhost";  
    $username="psaude";  
    $password="";  
    $db = "psaude_pedidos"; 
    $conn = new mysqli($hostname, $username, $password, $db);
    // Checa conexão
    if ($conn->connect_error) {
        die("Conexão falhou: " . $conn->connect_error);
    }
    $username_table = 'pedido_' . $chns_cliente . '_' . $PedidoID;
    $prepara_table = $conn->prepare("CREATE TABLE $username_table (`id` int(11) NOT NULL, `produto` longtext NOT NULL, `valor_org` longtext NOT NULL, `valor_desc` longtext NOT NULL, `quantidade` longtext NOT NULL, `valor_total_prod` longtext NOT NULL, `obs` longtext NOT NULL, `data_pedido` longtext NOT NULL, `chns` longtext NOT NULL, `para` longtext NOT NULL, `quem` longtext NOT NULL, `pedido` longtext NOT NULL,PRIMARY KEY (id))");
    $prepara_table_autoinc = $conn->prepare("ALTER TABLE $username_table MODIFY `id` int(11) NOT NULL AUTO_INCREMENT");
    $verifica_table = $prepara_table->execute();
    $verifica_table_autoinc = $prepara_table_autoinc->execute();
    // prepare e bind
    $stmt = $conn->prepare("INSERT INTO $username_table (produto,valor_org,valor_desc,valor_total_prod,quantidade,obs,data_pedido,chns,para,quem,pedido) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
    $stmt->bind_param("sssssssssss", $produto, $valor_org, $valor_desc, $valor_total_prod, $quantidade, $obs, $data_pedido, $chns, $para, $repre_chns, $pedido);
    for ( $a=0; $a<$quant; $a++ ) {
        $check = $array[$a]['quantidade'];
        if($check!=""){
            $produto = $array[$a]['produto'];
            $valor_org = $array[$a]['valor_org'];
            $total_desc = $array[$a]['valor_desc'];
            if ($total_desc == '') {
                $total_desc_item = "0";
            }else{
                $totals = $array[$a]['quantidade'];
                $totals2 = $array[$a]['valor_desc'];
                echo $totals . ' - '.$totals2;
                $total_desc_item =  $totals * $totals2;
            }
            $valor_desc = $total_desc;
            $valor_total_prod = $total_desc_item;
            $quantidade = $array[$a]['quantidade'];
            $obs = $obs_ped;
            $data_pedido = $data_pedido;
            $chns = $chns_ped;
            $para = $empresa_chns;
            $repre_chns = $quem;
            $pedido = $PedidoID;
            $stmt->execute();
        }
    }
    $stmt->close();
    $conn->close();
}
$PedidoID=rand(5, 150000);

1 answer

0

If in place of for use a foreach ($array as $produtoCarrinho) may solve your problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.