how to make a counter of the cart items [closed]

2

I want him to take my session cart and see how many products he has there he does some sort of for that will run while I have products in the cart, more specific it will run by the amount of the cart if I buy 3 products it will rotate 3x and so on and I want to get this variable $i and add according to the numbers of the pagseguro ex the pagseguro denominates the name of the items thus

<input name="itemId1" type="hidden" value="id_produto"> 

where is this itemId1 name it has to count like this:

<input name="itemId<?php echo $i; ?>" type="hidden" value="id_produto">

I've done more or less like this and it ends up in an infinite loop and giving a stick in the browser, can anyone help me?

the name of my session and SESSION['carrinho'] .

here and where I create and make the actions for it to add, delete etc.

session_start();

      if(!isset($_SESSION['carrinho'])){
         $_SESSION['carrinho'] = array();
      }

      //adiciona produto

      if(isset($_GET['acao'])){

         //ADICIONAR CARRINHO
         if($_GET['acao'] == 'add'){
            $id = intval($_GET['id']);
            if(!isset($_SESSION['carrinho'][$id])){
               $_SESSION['carrinho'][$id] = 1;
            }else{
               $_SESSION['carrinho'][$id] += 1;
            }
         }

         //REMOVER CARRINHO
         if($_GET['acao'] == 'del'){
            $id = intval($_GET['id']);
            if(isset($_SESSION['carrinho'][$id])){
               unset($_SESSION['carrinho'][$id]);
            }
         }

         //ALTERAR QUANTIDADE
         if($_GET['acao'] == 'up'){
            if(is_array($_POST['prod'])){
               foreach($_POST['prod'] as $id => $qtd){
                  $id  = intval($id);
                  $qtd = intval($qtd);
                  if(!empty($qtd) || $qtd <> 0){
                     $_SESSION['carrinho'][$id] = $qtd;
                  }else{
                     unset($_SESSION['carrinho'][$id]);
                  }
               }
            }
         }

      } 

that part and his foreach

foreach($_SESSION['carrinho'] as $id => $qtd){
                              $sql   = "SELECT *  FROM produtos WHERE id_produto= '$id'";
                              $qr    = mysql_query($sql) or die(mysql_error());
                              $ln    = mysql_fetch_assoc($qr);

                              $nome  = $ln['nome'];
                              $preco = $ln['preco'];
                              $sub   = $ln['preco'] * $qtd;
                              $img   = $ln['img'];
                              $desc  = $ln['descricao'];
                              $id    = $ln['id_produto']; 
                              $total += $ln['preco'] * $qtd;

And here is what I tried to do

<form target="pagseguro" method="post" action="https://pagseguro.uol.com.br/checkout/checkout.jhtml">
          <input type="hidden" name="email_cobranca" value="[email protected]">
          <input type="hidden" name="tipo" value="CP">
          <input type="hidden" name="moeda" value="BRL">
         <?php 
            for($i = 1; count($_SESSION['carrinho']) > 0; $i++){
         ?>
        <input name="itemId<?php echo $i; ?>" type="hidden" value="<?php echo $id[$i]; ?>">  
        <input name="itemDescription<?echo $i; ?>" type="hidden" value="<?php echo $nome[$i]; ?>">  
        <input name="itemAmount<?php echo $i; ?>" type="hidden" value="<?php echo $sub[$i];?>">  
        <input id="leo" name="itemQuantity<?php echo $i; ?>" type="hidden" value="1">  
        <input name="itemWeight<?php echo $i; ?>" type="hidden" value="1000">


          <button  class="btn btn-default check_out" id="comprar">Finalizar Compra</button>
          <?php } ?>
        </form>
    
asked by anonymous 23.11.2015 / 19:13

2 answers

0

Change:

for($i = 1; count($_SESSION['carrinho']) > 0; $i++){

To:

for($i = 1; $i<=count($_SESSION['carrinho']); $i++){

That way, as long as it is less than or equal to the cart count, it will loop.

    
23.11.2015 / 19:41
-1

Do this:

<form target="pagseguro" method="post" action="https://pagseguro.uol.com.br/checkout/checkout.jhtml">
<input type="hidden" name="email_cobranca" value="[email protected]">
<input type="hidden" name="tipo" value="CP">
<input type="hidden" name="moeda" value="BRL">
<?php 
if($_SESSION['carrinho']){
foreach ($_SESSION['carrinho'] as $key => $value){    
?>
    <input name="itemId<?php echo $key; ?>" type="hidden" value="<?php echo $value['id']; ?>">  
    <input name="itemDescription<?echo $key; ?>" type="hidden" value="<?php echo $value['nome']; ?>">  
    <input name="itemAmount<?php echo $key; ?>" type="hidden" value="<?php echo $value['sub'];?>">  
    <input id="leo" name="itemQuantity<?php echo $key; ?>" type="hidden" value="1">  
    <input name="itemWeight<?php echo $key; ?>" type="hidden" value="1000">
<?php } ?>

<button  class="btn btn-default check_out" id="comprar">Finalizar Compra</button>
<?php } ?>
</form>
    
23.11.2015 / 19:41