Switch case (JavaScript) inside a while loop (PHP)

237 views Asked by At

I have this JavaScript code, it works well, but when I tried to customize some math operations based on $id_math, it reads only the first row of the array inside the while loop, so calculate all the rows using the same $id_math, What changes do I need to do in my code, to get individual $id_math for every single row and perform the corresponding operation?.

$id_math is a php variable I use to store the value I got from my database. example: $id_math = 11, $operation_name = addition.

Here is my example.

Script

$(function(){

    CalculateTotal();
    // Adding the change events for the Price and 
    // quantity fields only..
    // Changing the total should not affect anything
    $('.quantity , .price').on('change', function() {
        UpdateTotals(this);
    });
});

function UpdateTotals(elem) {
    // This will give the tr of the Element Which was changed
    //in my code I have it like this 
   var abc=<?php echo id_math ?>;                   
    var $container = $(elem).parent().parent();
    var quantity = $container.find('.quantity').val();
    var price = $container.find('.price').val();
    var subtotal = parseFloat(quantity) + parseFloat(price);
    var subtotalmultiplication = parseFloat(quantity) * parseFloat(price);
    var subtotaldivision = parseFloat(quantity) / parseFloat(price);

    switch (abc) {
    case 12:
         $container.find('.subtotal').text(subtotal.toFixed(2));
    $container.find('.txtresult').val(subtotal.toFixed(2));
        break;
    case 11:
       $container.find('.subtotal').text(subtotalmultiplication.toFixed(2));
    $container.find('.txtresult').val(subtotalmultiplication.toFixed(2));
        break;
    case 13:
       $container.find('.subtotal').text(subtotaldivision.toFixed(2));
    $container.find('.txtresult').val(subtotaldivision.toFixed(2));
        break;

}

   //document.getElementById("txtresult").value = subtotal.toFixed(2);

}

function CalculateTotal(){
  // This will Itearate thru the subtotals and 
    // claculate the grandTotal and Quantity here

    var lineTotals = $('.subtotal');
    var quantityTotal = $('.quantity');
    var grandTotal = 0.0;
    var totalQuantity = 0;
    $.each(lineTotals, function(i){
        grandTotal += parseFloat($(lineTotals[i]).text()) ;
        totalQuantity += parseFloat($(quantityTotal[i]).val()) 
    });

    $('.totalquantity').text(totalQuantity);
    $('.grandtotal').text(parseFloat(grandTotal ).toFixed(2) );       

} 

php code

<table id="tabla-registros" class="table table-striped table-bordered bootstrap-datatable datatable responsive">

<thead>
<tr>
     <th>NÂș Reg</th>
     <th>Fecha</th>
     <th>Nombre Completo del Paciente</th>
     <th>Clave para la Descarga</th>
     <th>Estado del Registro</th>
     <th>Acciones</th>

</tr>
</thead>
<tbody>
    <?php

            $numrows = mysql_num_rows($list_results);

            if($numrows > 0){

                while($row=mysql_fetch_array($list_results)){
                $patient_name= utf8_encode($row['nombrepaciente']);
                $keyword= $row['llave'];
                $url_result= $row['resultado'];
                $comments= $row['observaciones'];
                $status_result= $row['nombre_estado'];
                $date_result= $row['fecha'];
                $id_status= $row['idestado'];
                $id_resultado= $row['idresultados'];
                $id_math = $row['idmath'];

                ?>
 <tr>
       <td><?php echo $id_resultado;?></td>
       <td><?php echo $date_result;?></td>
       <td><?php echo $patient_name;?></td>
       <td><?php echo $keyword;?></td>

            <?php switch($id_status){ 

                    case '3':?>
                        <td><span class="label-success label label-default"><?php echo $status_result; ?></span></td>
                            <?php  break; ?>

                        <?php   case '1':?>
                            <td>  <span class="label-warning label label-default"><?php echo $status_result; ?></span></td>
                        <?php  break;?>


                            <?php   case '2':?>
                            <td> <span class="label-default label label-danger"><?php echo $status_result; ?></span></td>
                        <?php  break; }?>

     <!--This is the block of the code where I need help because the javascript code only reads the first row of the while loop to perform the operation, but the second row has another id_math to calculate other operations -->
                       <table>
                           <tr>
                               <th width=200px>Description</th> 
                               <th width=60px>DATA1</th> 
                               <th width=60px>DATA2</th> 
                               <th width=60px>Total</th> 
                           </tr>
                            <tr>
                               <td width=200px>CALCULATE</td> 
                              <td width=60px><input type="text" value="15.99" class="price" /></td> 
                              <td width=60px><input type="text" value="1" class="quantity" /></td> 
                              <td width=60px><input type="text" value="" name="txtresult"  id= "txtresult" class="txtresult" /></td> 

                          </tr> 



                           </table>
0

There are 0 answers