<?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);
?>
问题:我有一个员工数据库表,在这里我要查询员工姓名和他们各自的日常工作。在我的表中,我每天为3名员工提供500,600和600的费率。当我在while循环中回显相应的费率时,它会针对每个员工显示正确的值。但是,当我乘以天数时,它似乎总是乘以600(尽管我的数据库中的第一个值是500)。感谢您是否可以指出我出错的地方。
答案 0 :(得分:2)
当然,你得到php_rate = 600
。
循环中的最后一次迭代会产生$Rate = 600 ;
。所以你只使用变量的最后一个值。
您必须将费率存储在数组中并使用它们,或者只是在while
循环中进行乘法运算。
另一种选择:将速率作为函数参数传递。
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;
}
另外,使用try - catch
阻止有什么问题?你试图抓住什么例外? mysql_fetch_array
不会抛出任何内容。
更多建议:
$array[$i]
等等。mysql_*
,不推荐使用。 mysqli_*
或PDO
现在是您的朋友。$rate
,$wage
(在JS中也是如此)echo "Look, I am so usable here! Name: {$variable}" ;