พี่ค๋า ทำทั้งวันทั้งคืนแล้ว ไปไม่รอดจิงๆ ถามอีกนิดเรื่องการคำนวณ 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">
<p>
<label for="textfield"></label>
<input type="text" name="sumpercent" id="sumpercent" />
</p>
<p>
<input type="submit" name="button" id="button" value="Submit" />
</p>
</form></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>
Tag : PHP, HTML/CSS, JavaScript
ประวัติการแก้ไข 2013-05-12 10:51:05
Date :
2013-05-12 10:50:20
By :
nongtoom
View :
1236
Reply :
16
คือจำนวนแถวมันไม่แน่นอนอะค่ะ มันแล้วแต่สมาชิกว่ามีประเภทนั้นกี่ประเภท อีกอย่างมันเป็น จาวาสคริบส์ด้วยหนูดูไม่ค่อยเป็น ต้องแก้ตรงไหน
Date :
2013-05-12 18:01:23
By :
nongtoom
ไม่มีไครตอบเลย เงียบจัง
Date :
2013-05-12 20:27:59
By :
nongtoom
อยากให้น้องลองเพิ่ม class ของ textbox ที่ต้องการคำนวนรวมทั้งหมดเป็น class ชื่ออะไรก็ได้สักชื่อหนึ่ง (สมมติชื่อ percent) แล้วทำประมาณนี้อ่าครับ
Code (JavaScript)
$(".percent").keyup(function(){
var sum = 0;
$(".withdraw").each(function(){
sum += parseFloat($(this).val()));
});
$("#sumpercent").attr("readonly",true).val(sum);
});
ประวัติการแก้ไข 2013-05-12 20:34:01
Date :
2013-05-12 20:33:45
By :
itpcc
class ใส่ไว้ใน textbox ทุกช่องที่ต้องการเก็บค่ามาคำนวน
script ใส่ไว้ตรงไหนก็ได้ แต่ต้องหลังจาก include jQuery library แล้ว
ประวัติการแก้ไข 2013-05-12 21:02:11
Date :
2013-05-12 20:58:43
By :
itpcc
ก็แล้วมันขึ้น error ว่าอะไรอ่าครับ
Date :
2013-05-12 22:04:31
By :
itpcc
<?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">
<p>
<label for="textfield"></label>
<input type="text" name="sumpercent" id="sumpercent" />
<input type="hidden" name="hdNum" id="hdNum" value="<?=$b;?>">
</p>
<p>
<input type="submit" name="button" id="button" value="Submit" />
</p>
</form></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 _hdNum = document.getElementById("hdNum").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 = "";
}
}
// sumpercent
var _sum=0;
for(var i=1;i<_hdNum;i++){
var _percent = document.getElementById("percent"+i).value;
if(isNaN(_percent)) _percent = 0;
_sum += (_percent * 1);
}
document.getElementById("sumpercent").value = _sum.toFixed(2);
}
</script>
</html>
Date :
2013-05-13 09:09:09
By :
thep
<?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">
<p>
<label for="textfield"></label>
<input type="text" name="sumpercent" id="sumpercent" />
<input type="hidden" name="hdNum" id="hdNum" value="<?=$b;?>">
</p>
<p>
<input type="submit" name="button" id="button" value="Submit" />
</p>
</form></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 _hdNum = document.getElementById("hdNum").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 = "";
}
}
// sumpercent
var _sum=0;
for(var i=1;i<_hdNum;i++){
var _percent = document.getElementById("percent"+i).value;
if(isNaN(_percent)) _percent = 0;
_sum += (_percent * 1);
}
document.getElementById("sumpercent").value = _sum.toFixed(2);
}
</script>
</html>
Date :
2013-05-13 12:37:37
By :
thep
เย้ ได้แล้ว รักพี่ thep มากกกกกกกกกกกกกกก ขอบคุณพี่มากกๆๆๆๆๆๆๆๆๆ
Date :
2013-05-13 13:02:30
By :
nongtoom
Load balance : Server 04