มีปัญหาการคำนวณ textbox หลายอัน textbox พร้อมกัน เหมือนมันคำนวณอยู่แค่ช่องเดียว
Code (PHP)
<?php
$objConnect = mysql_connect("localhost","root","1234") or die("Error Connect to Database");
$objDB = mysql_select_db("household");
mysql_query("SET character_set_results=utf8");
mysql_query("SET character_set_client=utf8");
mysql_query("SET character_set_connection=utf8");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<fieldset>
<legend align="center">จัดสรรรายจ่ายย้อนหลังในรอบ 3 เดือน</legend>
<p align="center"><? //include ("sum_amount.php");?>
<p align="center"><table width="994" height="109" border="1" align="center">
<tr>
<td width="60" rowspan="2" align="center" valign="middle">ลำดับที่</td>
<td width="159" rowspan="2" align="center" valign="middle">รายการรายจ่าย</td>
<td height="23" colspan="3" align="center" valign="middle">รายจ่ายในรอบ 3 เดือน</td>
<td width="143" rowspan="2" align="center" valign="middle">เฉลี่ยในรอบ 3 เดือน</td>
<td width="138" rowspan="2" align="center" valign="middle">เปอร์เซนต์<br />
ที่ต้องการลด</td>
<td width="138" rowspan="2" align="center" valign="middle">รายจ่ายต่อเดือน</td>
</tr>
<tr>
<td width="118" align="center" valign="middle"> </td>
<td width="118" align="center" valign="middle"> </td>
<td width="118" height="23" align="center" valign="middle"> </td>
</tr>
<? $b=1;
$table="select distinct Out_Type_ID from outcome WHERE Mem_ID LIKE '".$_SESSION['Mem_ID']."' ";
$table1=mysql_query($table);
while($table3=mysql_fetch_assoc($table1)){
$a="select * from outcome_type WHERE Out_Type_ID LIKE '".$table3['Out_Type_ID']."'";
$a1=mysql_query($a);
$a2 = mysql_fetch_array($a1);
?>
<tr>
<td height="27" align="center"><? echo $b; ?></td>
<td align="center"><? echo $a2['Out_Type_Name'];?></td>
<td align="right"><?
$ttt="SELECT SUM(Out_Amount) AS Sum_Amount FROM outcome WHERE Out_Date BETWEEN '".date('Y-m-d',strtotime('-3 month'))."' AND '".date('Y-m-d',strtotime('-2 month'))."' AND Out_Type_ID LIKE '".$table3['Out_Type_ID']."' AND Mem_ID LIKE '".$_SESSION['Mem_ID']."'";
$ttt1=mysql_query($ttt);
$ttt2 = mysql_fetch_array($ttt1);
echo $ttt2['Sum_Amount'];
?>
</td>
<td align="right">
<?
$tt="SELECT SUM(Out_Amount) AS Sum_Amount FROM outcome WHERE Out_Date BETWEEN '".date('Y-m-d',strtotime('-2 month'))."' AND '".date('Y-m-d',strtotime('-1 month'))."' AND Out_Type_ID LIKE '".$table3['Out_Type_ID']."' AND Mem_ID LIKE '".$_SESSION['Mem_ID']."'";
$tt1=mysql_query($tt);
$tt2 = mysql_fetch_array($tt1);
echo $tt2['Sum_Amount'];
?>
</td>
<td align="right">
<?
$t="SELECT SUM(Out_Amount) AS Sum_Amount FROM outcome WHERE Out_Date BETWEEN '".date('Y-m-d',strtotime('-1 month'))."' AND '".date('Y-m-d')."' AND Out_Type_ID LIKE '".$table3['Out_Type_ID']."' AND Mem_ID LIKE '".$_SESSION['Mem_ID']."'";
$t1=mysql_query($t);
$t2 = mysql_fetch_array($t1);
echo $t2['Sum_Amount'];
?>
</td><form action="<?=$_SERVER["PHP_SELF"];?>" method="POST">
<td align="right"><? $so=$ttt2['Sum_Amount']+$tt2['Sum_Amount']+$t2['Sum_Amount']; $so1=$so/3; echo $so1;?></td>
<input type="hidden" name="total" id="total" value="<?=$so1?>" />
<td align="right"><input name="withdraw<?=$b;?>" id="withdraw<?=$b;?>" type="text" value="0" onKeyUp="calculate('<?=$b;?>','wd');"/></td>
<td align="right"><input type="text" name="percent<?=$b;?>" id="percent<?=$b;?>" value="0.0" onKeyUp="calculate('<?=$b;?>','pc');"/></td>
</form>
<?
$b++;
}
?>
<tr>
<td height="24"> </td>
<td>รวม</td>
<td align="right"></td>
<td align="right"></td>
<td align="right"></td>
<td align="right"></td>
<td align="right"></td>
<td align="right"> </td>
</tr>
</table>
<p><a href="index_user.php">กลับ</a></p>
</fieldset>
</body>
<script language="javascript">
function calculate(_id,_type){
var _hdTotal = document.getElementById("total").value;
var _result;
if (_type == "wd") { // withdraw
var _withdraw = document.getElementById("withdraw"+_id).value;
if(isNaN(_withdraw)) _withdraw = 0;
if (_withdraw != 0) {
_result = ((_hdTotal * (100 - _withdraw))/ 100);
document.getElementById("percent"+_id).value = _result.toFixed(2);
}
else {
document.getElementById("percent"+_id).value = "";
}
}
else if (_type == "pc") { // percent
var _percent = document.getElementById("percent"+_id).value;
if(isNaN(_percent)) _percent = 0;
if (_percent != 0) {
_result = (100 - ((_percent *100 ) / _hdTotal));
document.getElementById("withdraw"+_id).value = _result.toFixed(2);
}
else{
document.getElementById("withdraw"+_id).value = "";
}
}
}
</script>
</html>
Date :
2013-05-10 15:31:05
By :
thep
Code (PHP)
<?php
$objConnect = mysql_connect("localhost","root","1234") or die("Error Connect to Database");
$objDB = mysql_select_db("household");
mysql_query("SET character_set_results=utf8");
mysql_query("SET character_set_client=utf8");
mysql_query("SET character_set_connection=utf8");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<fieldset>
<legend align="center">จัดสรรรายจ่ายย้อนหลังในรอบ 3 เดือน</legend>
<p align="center"><? //include ("sum_amount.php");?>
<p align="center"><table width="994" height="109" border="1" align="center">
<tr>
<td width="60" rowspan="2" align="center" valign="middle">ลำดับที่</td>
<td width="159" rowspan="2" align="center" valign="middle">รายการรายจ่าย</td>
<td height="23" colspan="3" align="center" valign="middle">รายจ่ายในรอบ 3 เดือน</td>
<td width="143" rowspan="2" align="center" valign="middle">เฉลี่ยในรอบ 3 เดือน</td>
<td width="138" rowspan="2" align="center" valign="middle">เปอร์เซนต์<br />
ที่ต้องการลด</td>
<td width="138" rowspan="2" align="center" valign="middle">รายจ่ายต่อเดือน</td>
</tr>
<tr>
<td width="118" align="center" valign="middle"> </td>
<td width="118" align="center" valign="middle"> </td>
<td width="118" height="23" align="center" valign="middle"> </td>
</tr>
<? $b=1;
$table="select distinct Out_Type_ID from outcome WHERE Mem_ID LIKE '".$_SESSION['Mem_ID']."' ";
$table1=mysql_query($table);
while($table3=mysql_fetch_assoc($table1)){
$a="select * from outcome_type WHERE Out_Type_ID LIKE '".$table3['Out_Type_ID']."'";
$a1=mysql_query($a);
$a2 = mysql_fetch_array($a1);
?>
<tr>
<td height="27" align="center"><? echo $b; ?></td>
<td align="center"><? echo $a2['Out_Type_Name'];?></td>
<td align="right"><?
$ttt="SELECT SUM(Out_Amount) AS Sum_Amount FROM outcome WHERE Out_Date BETWEEN '".date('Y-m-d',strtotime('-3 month'))."' AND '".date('Y-m-d',strtotime('-2 month'))."' AND Out_Type_ID LIKE '".$table3['Out_Type_ID']."' AND Mem_ID LIKE '".$_SESSION['Mem_ID']."'";
$ttt1=mysql_query($ttt);
$ttt2 = mysql_fetch_array($ttt1);
echo $ttt2['Sum_Amount'];
?>
</td>
<td align="right">
<?
$tt="SELECT SUM(Out_Amount) AS Sum_Amount FROM outcome WHERE Out_Date BETWEEN '".date('Y-m-d',strtotime('-2 month'))."' AND '".date('Y-m-d',strtotime('-1 month'))."' AND Out_Type_ID LIKE '".$table3['Out_Type_ID']."' AND Mem_ID LIKE '".$_SESSION['Mem_ID']."'";
$tt1=mysql_query($tt);
$tt2 = mysql_fetch_array($tt1);
echo $tt2['Sum_Amount'];
?>
</td>
<td align="right">
<?
$t="SELECT SUM(Out_Amount) AS Sum_Amount FROM outcome WHERE Out_Date BETWEEN '".date('Y-m-d',strtotime('-1 month'))."' AND '".date('Y-m-d')."' AND Out_Type_ID LIKE '".$table3['Out_Type_ID']."' AND Mem_ID LIKE '".$_SESSION['Mem_ID']."'";
$t1=mysql_query($t);
$t2 = mysql_fetch_array($t1);
echo $t2['Sum_Amount'];
?>
</td><form action="<?=$_SERVER["PHP_SELF"];?>" method="POST">
<td align="right"><? $so=$ttt2['Sum_Amount']+$tt2['Sum_Amount']+$t2['Sum_Amount']; $so1=$so/3; echo $so1;?></td>
<input type="hidden" name="total<?=$b;?>" id="total<?=$b;?>" value="<?=$so1?>" />
<td align="right"><input name="withdraw<?=$b;?>" id="withdraw<?=$b;?>" type="text" value="0" onKeyUp="calculate('<?=$b;?>','wd');"/></td>
<td align="right"><input type="text" name="percent<?=$b;?>" id="percent<?=$b;?>" value="0.0" onKeyUp="calculate('<?=$b;?>','pc');"/></td>
</form>
<?
$b++;
}
?>
<tr>
<td height="24"> </td>
<td>รวม</td>
<td align="right"></td>
<td align="right"></td>
<td align="right"></td>
<td align="right"></td>
<td align="right"></td>
<td align="right"> </td>
</tr>
</table>
<p><a href="index_user.php">กลับ</a></p>
</fieldset>
</body>
<script language="javascript">
function calculate(_id,_type){
var _hdTotal = document.getElementById("total"+_id).value;
var _result;
if (_type == "wd") { // withdraw
var _withdraw = document.getElementById("withdraw"+_id).value;
if(isNaN(_withdraw)) _withdraw = 0;
if (_withdraw != 0) {
_result = ((_hdTotal * (100 - _withdraw))/ 100);
document.getElementById("percent"+_id).value = _result.toFixed(2);
}
else {
document.getElementById("percent"+_id).value = "";
}
}
else if (_type == "pc") { // percent
var _percent = document.getElementById("percent"+_id).value;
if(isNaN(_percent)) _percent = 0;
if (_percent != 0) {
_result = (100 - ((_percent *100 ) / _hdTotal));
document.getElementById("withdraw"+_id).value = _result.toFixed(2);
}
else{
document.getElementById("withdraw"+_id).value = "";
}
}
}
</script>
</html>
Date :
2013-05-10 15:39:31
By :
thep
ลองดูกระทู้นี้นะครับ
https://www.thaicreate.com/php/forum/094499.html
กำหนด id ให้ input ทุกตัวครับ เวลาคำนวณอ้างอิงเลขไอดีของแต่ละแถว
สร้างฟังก์ชั่นที่รับ id มาคำนวณ ตัวอย่าง
Code (JavaScript)
function textsum(rowId){
var amount = document.getElementById('amount'+rowId).value;
var price = document.getElementById('price'+rowId).value;
var total = amount*price;
document.getElementById('total'+rowId).value = total;
}
ประวัติการแก้ไข 2013-05-10 16:10:29
Date :
2013-05-10 16:09:31
By :
Naizan
ขอบคุณพี่ๆทุกคนมากๆๆๆๆๆๆๆๆๆๆๆๆๆที่สุดในโลก โดยเฉพาะ พี่ thep ค่ะหนูทำมาหลายวันพี่เนี้ยะเซียนจริงๆ
Date :
2013-05-10 21:58:18
By :
nongtoom
เสียได้กด Like ไม่ได้ซะงั้น
Date :
2013-05-11 07:33:41
By :
mr.win
Load balance : Server 04