user2527186
user2527186

Reputation: 23

Issue with multiplication in php

<?php 

include 'dbconnection.php'; 

 $Days=0; $Kg=0.00; $Wage=0.00; 
 $Rate=0.00;  $KgDivDays=0.00; $ExtraKgs = 0.00;
 $WageExtraKgs=0.00; $TotalWages=0.00;
 $BalanceWage=0.00; $EightPerc=0.00; $TwelvePerc=0.00; $ThreePerc=0.00;
 $Advances=0.00; $FestAdvances=0.00;

//execute the SQL query and return records
$result = mysql_query("SELECT EmployeeName,DailyRate FROM employees");


//fetch the data from the database 
try{
$i = 0;
while ($row = mysql_fetch_array($result)) {
    echo "<tr><td>".$row{'EmployeeName'}."</td>";
    $Rate=$row{'DailyRate'};
    //echo "<td>".$Rate."</td>";
    //echo "<td style = 'display:none'>".$row{'DailyRate'}."</td>";
    //echo $Rate;
    echo "<td><input type='text' name='Days' id='Days".$i."' onChange='CalcWage(".$i.");'/></td>";
    echo "<td id='Wage".$i."'>".$Wage."</td>";
    echo "<td><input type='text' name='Kg' id='Kg".$i."' onChange='CalcAmount(".$i.");'/></td>";
    echo "<td id='KgDivDays".$i."'>".$KgDivDays."</td>";
    echo "<td id='ExtraKgs".$i."'>".$ExtraKgs."</td>";
    echo "<td id='WageExtraKgs".$i."'>".$WageExtraKgs."</td>";
    echo "<td id='TotalWages".$i."'>".$TotalWages."</td>";
    echo "<td id='EightPerc".$i."'>".$EightPerc."</td>";
    echo "<td id='TwelvePerc".$i."'>".$TwelvePerc."</td>";
    echo "<td id='ThreePerc".$i."'>".$ThreePerc."</td>";
    echo "<td><input type='text' name='Advances' id='Advances".$i."'/></td>";
    echo "<td><input type='text' name='FestAdvances' id='FestAdvances".$i."' onChange='CalcBalanceWage(".$i.");'/></td>";
    echo "<td id='BalanceWage".$i."'>".$BalanceWage."</td>";
    $i++;
}
}
catch(Exception $e)
{echo "error".$e;}
?>

<script type="text/javascript">

function CalcWage(i){
    var Days=document.getElementById('Days'+i).value;
    var php_rate = "<?php echo $Rate; ?>";
    //alert(php_rate);
    var Wage=parseFloat(Days*php_rate);
    document.getElementById('Wage'+i).innerHTML= Wage;
    return Wage;
}

function CalcAmount(i){
    var Kg=document.getElementById('Kg'+i).value;
    var Days=document.getElementById('Days'+i).value;
    var KgDivDays = parseFloat(Days*4);
    document.getElementById('KgDivDays'+i).innerHTML= KgDivDays;
    var ExtraKgs=Kg-KgDivDays;
    document.getElementById('ExtraKgs'+i).innerHTML= ExtraKgs;
    var WageExtraKgs=ExtraKgs*50;
    document.getElementById('WageExtraKgs'+i).innerHTML= WageExtraKgs;
    var BasicSal = CalcWage(i);
    var TotalWages= BasicSal + WageExtraKgs;
    document.getElementById('TotalWages'+i).innerHTML= TotalWages;
    var EightPerc= parseFloat(BasicSal * 0.08);
    document.getElementById('EightPerc'+i).innerHTML= EightPerc;
    var TwelvePerc= parseFloat(BasicSal * 0.12);
    document.getElementById('TwelvePerc'+i).innerHTML= TwelvePerc;
    var ThreePerc= parseFloat(BasicSal * 0.03);
    document.getElementById('ThreePerc'+i).innerHTML= ThreePerc;
    return TotalWages;
}

function CalcBalanceWage(i){
    var Advances=parseFloat(document.getElementById('Advances'+i).value);
    var FestAdvances=parseFloat(document.getElementById('FestAdvances'+i).value);
    var TotWage = CalcAmount(i); 
    var BasSal =  CalcWage(i); 
    var Deductions = parseFloat(Advances+FestAdvances+(BasSal*0.08));
    var BalanceWage=parseFloat(TotWage-Deductions);
    document.getElementById('BalanceWage'+i).innerHTML= BalanceWage;
    return BalanceWage;
}

</script>

<?php
//close the connection
mysql_close($dbhandle);
?>

Question: I have a database table for employees and here i am querying to get the employee name and their respective dailyrates. In my table I have daily rates 500, 600 and 600 for the 3 employees. When I echo the respective rates on my while loop it displays the correct value against each employee. However, when I multiply by Days it seems to always multiply by 600 (although the 1st value in my db is 500). Appreciate if you can point me to where I have gone wrong.

Upvotes: 1

Views: 132

Answers (1)

sybear
sybear

Reputation: 7784

Of course you get php_rate = 600.

The last iteration in your loop makes $Rate = 600 ;. So you use only the last value of the variable.

You must either store your rates in array and the use them, or just do the multiplication inside your while loop.

Another option: pass the rate as the function parameter.

onClick = 'CalcWage({$i}, {$Rate});'

function CalcWage(i, php_rate){
    var Days=document.getElementById('Days'+i).value;
    //var php_rate = "<?php echo $Rate; ?>";
    //alert(php_rate);
    var Wage=parseFloat(Days*php_rate);
    document.getElementById('Wage'+i).innerHTML= Wage;
    return Wage;
}

Also, what is wrong with using try - catch block? What exception are you trying to catch? mysql_fetch_array does not throw any.

More suggestions:

  • Access array data using square brackets. $array[$i] and so on.
  • Do not use mysql_*, it is deprecated. mysqli_* or PDO are your friends now.
  • Variable names should start with a small letter. $rate, $wage (in JS too)
  • When concatenating a string, you do not have to break it into pieces. You could just put variables like that: echo "Look, I am so usable here! Name: {$variable}" ;

Upvotes: 2

Related Questions