|
 |
|
ปัญหา การ คำนวณ totalprice ของสินค้าคำนวณ สินค้าที่มี 1 Recordไม่ได้ แต่คำนวณที่มีมากกว่า 1 Record ได้ ขอทราบวิธีแก้ทีคับ |
|
 |
|
|
 |
 |
|
ปัญหาคือ ถ้าเราเลือก บริษัทคู่ค้าจาก list ที่มี ข้อมูลมากกว่า 1recordคือ 2ขึ้นไป ก็จะคำนวณ ยอดรวมต่างๆได้ แต่ถ้าเลือก แล้วมีข้อมูลแค่ 1record ก็จะคำนวณไม่ได้
Code (JavaScript)
function calcTotals()
{
var grandTotal = 0;
var row = 0;
while (document.forms['form1'].elements['numpro[]'][row])
{
priceObj = document.forms['form1'].elements['numpro[]'][row];
qtyObj = document.forms['form1'].elements['numper[]'][row];
totalObj = document.forms['form1'].elements['total[]'][row];
if (isNaN(priceObj.value))
{
priceObj = '';
}
if (isNaN(qtyObj.value))
{
qtyObj = '';
}
if (priceObj.value && qtyObj.value)
{
totalObj.value = (parseFloat(priceObj.value) * parseFloat(qtyObj.value));
grandTotal = grandTotal + parseFloat(totalObj.value);
}
else
{
totalObj.value = '';
}
row++;
}
document.getElementById('grand_total').value = grandTotal;
return;
}
<form name="form1" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="POST" >
<label>รหัสใบเสนอ</label> <input type="text" size="15px" name="quoteid" readonly="readonly" value = "<?php echo $result1 ?>"/ >
<label>เสนอโดย</label> <input type="text" size="15px" name="empid" value = "<?php echo $result2['EMP_NAME']?> <?php echo $result2['EMP_LNAME'] ?>" readonly="readonly">
<label>วันที่</label> <input type = "text" size="15px" name="date" value = "<?php echo date("d-m-Y"); ?>" readonly="readonly"></br>
<label>บริษัทคู่ค้า</label> <select name="company" onchange="loadData(this.options[this.selectedIndex].value)">
<option value="-2">Select Company</option>
<option value="-1">Select All</option>
<?php
$sql = "SELECT * FROM company order by COM_NAME asc";
$obj->query($sql);
while($objResult = $obj->fetch_array()){
?>
<option value="<?php echo $objResult['COM_ID']?>" ><?php echo $objResult['COM_NAME']?></option>
<?php
}
?>
</select>
<div id="txtHint"></div></br>
<div>ราคารวมทั้งหมด<input type="text" id="grand_total"readonly="readonly" value="0"></div>
<input type="submit" name ="submit" value ="เพิ่มรายการเสนอ"/>
<button type="button" onClick="location.href='main_page.php'" >ยกเลิก</button></br>
</form>
Code (PHP)
<?php
require('Core.php');
$obj = new Core;
if(!isset($_POST['loadId']) )
{
echo "<script language='javascript'> alert('No Data');</script>";
}
$loadId = $_POST['loadId'];
if(isset($_POST['loadId']))
{
if($_POST['loadId'] != -1)
{
$sql = "SELECT * FROM product,product_color,model,brand,product_type,color WHERE product.COM_ID = '".$loadId."' and product.PRO_ID = product_color.PRO_ID and
product.MODEL_ID = model.MODEL_ID and product.BRAND_ID = brand.BRAND_ID and product.PRO_TYPE_ID = product_type.PRO_TYPE_ID and product_color.COLOR_ID = color.COLOR_ID";
$tmp = $obj->query($sql);
$check = $obj->num_rows();
}
else
$sql = "SELECT * FROM product,product_color,model,brand,product_type,color WHERE product.PRO_ID = product_color.PRO_ID and
product.MODEL_ID = model.MODEL_ID and product.BRAND_ID = brand.BRAND_ID and product.PRO_TYPE_ID = product_type.PRO_TYPE_ID and product_color.COLOR_ID = color.COLOR_ID";
$tmp = $obj->query($sql);
$check = $obj->num_rows();
}
else
echo "<script language='javascript'>alert('Error ');window.location='quote.php';</script>";
if($check > 0)
{
$HTML="";
echo "<table width='75%'' border='1' cellspacing='2' cellpadding='0'>
<tr>
<th>รหัสสินค้า</th>
<th>ประเภท</th>
<th>ยี่ห้อ</th>
<th>รุ่น</th>
<th>สี</th>
<th>จำนวนที่เสนอ</th>
<th>ราคา/หน่วย</th>
<th>ราคารวม</th>
</tr>";
while($row = $obj->fetch_array()){
$pro = $row['PRO_ID'];
echo "<tr>";
echo "<td align='center' >" .$row['PRO_ID'] . "</td>";
echo "<td align='center'>" . $row['PRO_TYPE_NAME'] . "</td>";
echo "<td align='center'>" . $row['BRAND_NAME'] . "</td>";
echo "<td align='center'>" . $row['MODEL_NAME'] . "</td>";
echo "<td align='center'>" . $row['COLOR_NAME'] . "</td>";
echo "<td align='center'>" .'<input type="number" name="numpro[]" onchange="calcTotals()" >' . "</td>";
echo "<td align='center'>" .'<input type="number" name="numper[]" onchange="calcTotals()" >' . "</td>";
echo "<td align='center'>" .'<input type="number" name="total[]" id="total" readonly="readonly">' . "</td>";
echo "<td>" .'<input type="checkbox" name="select[]" id="select" ' .'onclick="checkAll(this)" value ="'. $pro .'" >'. "</td>";
}
echo "</table>";
echo $HTML;
}
?>
Code
Tag : PHP, HTML/CSS, JavaScript, Ajax

|
ประวัติการแก้ไข 2014-12-01 21:55:47
|
 |
 |
 |
 |
Date :
2014-12-01 21:51:47 |
By :
top113457 |
View :
805 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อ่านโค๊ดแล้วงงครับ
numpro[] numper[] total[] อยู่ใน form1 ตรงไหนครับ
ถึงได้เขียนอ้างอิง docuement.forms[form1].elements[..] ได้ครับ
|
 |
 |
 |
 |
Date :
2014-12-02 09:18:42 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|