Help the amount value is stored for all the stock?

0

I am making a shopping cart via web and it is practically finished the problem to verify that the amount is equal to the stock this throws me a message saying exceeded the limit, but that happens with all those who have the same stock as another option they recommend me.

 <script>

$(document).ready(function(){



$('.btcar').click(function(e){
    e.preventDefault();
    e.isPropagationStopped();
    var car = $(this).attr("id");
    var stock = parseInt ($('#stock'+car).val());
    var cant = parseInt($('#canti'+car).val());




    if(stock != cant ){

    $.ajax({

        url:'carrito.php',
        method: 'POST',
        data:{car:car},
        type:JSON,
        success:function(deta){

            $('#carrito').html(deta);
            $('#resultado').html(deta);
        }

    });

 } else{

     alert("Excedió el límite");

 }});  


$('#carrito').load("carrito.php");


    });
</script>

- this is the one of the cart

<article>
            <?php 
    if(isset ($_SESSION['usuario1'])){


    ?>
            <!-----ARREGLO MULTIDIMENCIONAL------>
            <?php
       if(isset($_POST['car'])){
       $id= $_POST['car'];
       $cantidad=1;
       $sql3="SELECT * FROM productos WHERE id = '$id'";
       $query3= mysqli_query($conexion,$sql3);
       $row3 = mysqli_fetch_array($query3);

       $id= $row3['id'];
                $imagen= $row3['imagen'];
                $nombre= $row3['nombre'];
                $descripcion= $row3['descripcion'];
                $precio= $row3['precio'];
                $stock= $row3['stock'];
                $fecha= $row3['fecha'];
                $hora= $row3['hora'];
                $categoria=$row3['categoria'];
       $sucursal=$row3['sucursal'];
                $cantidad;


     $shopCart[]= array('id'=> $id,'imagen'=> $imagen,'nombre'=>   $nombre,'descripcion'=> $descripcion,'precio'=> $precio, 'stock'=> $stock, 'fecha'=> $fecha, 'hora'=> $hora, 'categoria'=> $categoria, 'sucursal'=> $sucursal, 'cantidad'=> $cantidad);

   }       

        if(isset($_SESSION['carrito'])){
           $shopCart = $_SESSION['carrito'];
            if(isset($_POST['car'])){

                 $sql3="SELECT * FROM productos WHERE id = '$id'";
       $query3=mysqli_query($conexion,$sql3);
       $row3= mysqli_fetch_array($query3);


               $id= $row3['id'];
                $imagen= $row3['imagen'];
                $nombre= $row3['nombre'];
                $descripcion= $row3['descripcion'];
                $precio= $row3['precio'];
                $stock= $row3['stock'];
                $fecha= $row3['fecha'];
                $hora= $row3['hora'];
                $categoria=$row3['categoria'];
                $sucursal=$row3['sucursal'];
                $cantidad;
                $posicion = -1;   


            for($i=0; $i<count($shopCart);$i++){
                if($id == $shopCart[$i]['id']){
                    $posicion = $i;  
                }
                          }  

                if($posicion != -1){
                    $cantidad_nueva = $shopCart[$posicion]['cantidad'] + $cantidad;
                    $shopCart[$posicion] = array('id'=> $id,'imagen'=> $imagen,'nombre'=> $nombre,'descripcion'=> $descripcion,'precio'=> $precio, 'stock'=> $stock, 'fecha'=> $fecha, 'hora'=>$hora, 'categoria'=> $categoria, 'sucursal'=> $sucursal, 'cantidad'=> $cantidad_nueva);

                }else{
                    $shopCart[]= array('id'=> $id,'imagen'=> $imagen,'nombre'=> $nombre,'descripcion'=> $descripcion,'precio'=> $precio, 'stock'=> $stock, 'fecha'=> $fecha, 'hora'=>$hora, 'categoria'=> $categoria, 'sucursal'=> $sucursal, 'cantidad'=> $cantidad);

                }

            }
        }

        // NUEVOS PROCESOS...

        // PROCESO PARA ACTUALIZAR PRODUCTO

    if(isset($_POST['idupdate'])){

      $idupdate= $_POST['idupdate'];
      $cantidad= $_POST['cantidad'];
      $shopCart[$idupdate]['cantidad']= $cantidad;
  }      



        // Proceso para eliminar productos

        if(isset($_POST['ideliminar'])){
            $ideliminar= $_POST['ideliminar'];
      $shopCart[$ideliminar] = NULL;


        }

        if(isset($shopCart)){

            $_SESSION['carrito'] = $shopCart;
        }



?>

                <table class="table">

                    <thead>
                        <tr>
                           <th class="th"></th>
                            <th class="th">Imagen</th>
                            <th class="th">Nombre</th>
                            <th class="th">Precio</th>
                            <th class="th">Cantidad</th>
                            <th class="th">Subtotal</th>
                            <th></th>&nbsp;</tr>
                    </thead>


                           <tbody >
                            <?php 
         if(isset($shopCart)){
             $total = 0;
             for($i=0; $i<count($shopCart); $i++){

             if($shopCart[$i] != NULL){

          ?> 
                           <tr >

                           <td>  <?php echo $shopCart[$i]['sucursal'] ?> </td>
                            <td><img src="img/productos/<?php echo $shopCart[$i]['imagen'] ?>" width="150" height="150"></td>
                            <td>
                                <?php echo $shopCart[$i]['nombre'] ?>
                            </td>
                            <td>
                                <?php echo number_format ( $shopCart[$i]['precio'], 0, '', '.'); ?>
                            </td>
                            <td>

                                <form method="post" id="formstock">
                                     <input type="hidden" value="<?php echo $shopCart[$i]['stock']; ?>">

                                </form>


                                <form method="post" class="formcantidad" >

                                   <input type="hidden" id="prue" value="<?php echo $i ?>">
                                    <input type="text" class="canti" id="canti<?php echo $shopCart[$i]['stock'] ?>" name="cantidad" min="1" maxlength="<?php echo $shopCart[$i]['stock']; ?>" value="<?php echo $shopCart[$i]['cantidad']; ?>" style="text-alaing: center;">

                                    <br>
                                </form>



                            </td>

                            <!------------- aqui guardan los calculos de los precios--->
                            <td>
                                <?php 
             $subtotal = $shopCart[$i]['precio']*$shopCart[$i]['cantidad'];
                 $total=$subtotal+$total;
                 echo 'Bs.S '; echo number_format ($subtotal, 0, '', '.');

             ?>
                            </td>
                            <td >
                                <form >
                                    <input type="hidden" name="ideliminar" value="<?php echo $i;?>">
                                    <button type="button" class="eliminarbt" id="<?php echo $i;?>"> <span class="fa fa-times" value="Eliminar" src="#" style="color:red; cursor:pointer;"> </span> </button>
                               </form>
                            </td>
                        </tr>
                        <?php }}} ?>
                        <tr>
                            <td colspan="4"> &nbsp;


                            </td>

                            <td class="resultado"> <strong>Total </strong><br> Bs.S:
                             <?php echo @number_format ($total, 0, '', '.'); ?>   <br>


                                    <input type="hidden" id="total" name="total" value="<?php echo  @$total ; ?>">
                                    <button type="button" class="btsiguiente" src="#" style="width: 135px; height:40px;" value="Comprar"> <span class="fa fa-check" style="color:green; cursor:pointer;"> Siguiente</span></button>




                            </td>


                        </tr>

                    </tbody>



                </table>
                <br><br>
                <br>






                <?php

    }



            else{

        echo '<script>

        alert("Debes Iniciar Sesion Para Poder Realizar Esta Transicción");
        window.location.href="valencia-tocuyito.html";

        </script>';

    }

    ?>
        </article>
    
asked by Steban De Abreu 16.10.2018 в 16:47
source

1 answer

0

Your problem is here:

<input type="text" class="canti" id="canti<?php echo $shopCart[$i]['stock'] ?>" name="cantidad" min="1" maxlength="<?php echo $shopCart[$i]['stock']; ?>" value="<?php echo $shopCart[$i]['cantidad']; ?>" style="text-alaing: center;">

You are assigning the stock value to id. If two products have the same amount in stock then the two will generate the message. Instead of using stock, use the variable $i like this:

<input type="text" class="canti" id="canti<?php echo $i ?>" name="cantidad" min="1" maxlength="<?php echo $shopCart[$i]['stock']; ?>" value="<?php echo $shopCart[$i]['cantidad']; ?>" style="text-alaing: center;">
    
answered by 16.10.2018 в 17:00