Page 1 of 1

ถามอีกนิดเรื่องการคำนวณ textbox หลาย textbox พร้อมกัน

Posted: 12/05/2013 10:22 pm
by charisma111
คืออยากรวมค่าที่อยู่ใน textbox ในกรอบสีแดงทั้งหมดมาใส่ในอีก textbox รวมอัตโนมัติ

Code: Select all

<?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">&nbsp;</td>
      <td width="118" align="center" valign="middle">&nbsp;</td>
      <td width="118" height="23" align="center" valign="middle">&nbsp;</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">&nbsp;</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>