Select option คำนวณ ให้ คูณ แบบ Apto ทำงัยครับผม
ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา Select option คำนวณ ให้ คูณ แบบ Apto ทำงัยครับผม
Select option คำนวณ ให้ คูณ แบบ Apto ทำงัยครับผม
1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | <?php session_start(); require 'connect.php' ; $action = isset( $_GET [ 'a' ]) ? $_GET [ 'a' ] : "" ; $itemCount = isset( $_SESSION [ 'cart' ]) ? count ( $_SESSION [ 'cart' ]) : 0; if (isset( $_SESSION [ 'qty' ])) { $meQty = 0; foreach ( $_SESSION [ 'qty' ] as $meItem ) { $meQty = $meQty + $meItem ; } } else { $meQty = 0; } if (isset( $_SESSION [ 'cart' ]) and $itemCount > 0) { $itemIds = "" ; foreach ( $_SESSION [ 'cart' ] as $itemId ) { $itemIds = $itemIds . $itemId . "," ; } $inputItems = rtrim( $itemIds , "," ); $meSql = "SELECT * FROM childtb WHERE id in ({$inputItems})" ; $meQuery = mysql_query( $meSql ); $meCount = mysql_num_rows( $meQuery ); } else { $meCount = 0; } ?> <form action= "updatecart.php" method= "post" name= "fromupdate" > <table width= "100%" height= "154" border= "1" align= "center" cellpadding= "0" cellspacing= "0" bordercolor= "#C76A32" class = "table-responsive" ;> <center> <tbody> <tr> <td width= "150" height= "50" bgcolor= "#FD6600" align= "center" ></td> <td width= "120" height= "50" bgcolor= "#FD6600" align= "center" >ชื่อ</td> <td width= "200" height= "50" bgcolor= "#FD6600" align= "center" >รายละเอียด</td> <td width= "100" height= "50" bgcolor= "#FD6600" align= "center" >อุปการะต่อ/เดือน</td> <!--<td width= "100" height= "50" bgcolor= "#FD6600" align= "center" >อุปการะ</td>--> <td width= "100" height= "50" bgcolor= "#FD6600" align= "center" >รวม</td> <td width= "100" height= "50" bgcolor= "#FD6600" align= "center" >ยกเลิกรายการ</td> </tr> </thead> <tbody> <?php $total_price = 0; $num = 0; while ( $meResult = mysql_fetch_assoc( $meQuery )) { $key = array_search ( $meResult [ 'id' ], $_SESSION [ 'cart' ]); $total_price = $total_price + ( $meResult [ 'product_price' ] * $_SESSION [ 'qty' ][ $key ]); ?> <tr> <td><img src= "child_img/<?php echo $meResult['product_img_name']; ?>" border= "0" ></td> <td width= "56" height= "30" align= "center" ><?php echo $meResult [ 'product_name' ]; ?></td> <td width= "56" height= "30" align= "center" ><?php echo $meResult [ 'product_desc' ]; ?></td> <td width= "56" height= "30" align= "center" > <select name= "month" id= "month" class = "form-control" value= "<?php echo $_SESSION['qty'][$key]; ?>" class = "form-control" style= "width: 60px;text-align: center;" > <option value= "1" >1</option> <option value= "3" >3</option> <option value= "6" >6</option> <option value= "12" >12</option> </select> <input type= "text" name= "qty[<?php echo $num; ?>]" value= "<?php echo $_SESSION['qty'][$key]; ?>" class = "form-control" style= "width: 60px;text-align: center;" > <input type= "hidden" name= "arr_key_<?php echo $num; ?>" value= "<?php echo $key; ?>" > </td> <!-- <td width= "56" height= "30" align= "center" ><?php echo number_format( $meResult [ 'product_price' ]); ?></td>--> <td width= "56" height= "30" align= "center" > <?php echo number_format(( $meResult [ 'product_price' ] * $_SESSION [ 'qty' ][ $key ] )); ?></td> <td width= "56" height= "30" align= "center" ><a href= "removecart.php?itemId=<?php echo $meResult['id']; ?>" > <span class = "text-danger" ><img src= "img/delete.png" ></span></a></td> </tr> <?php $num ++; } ?> <tr> <td colspan= "8" style= "text-align: right;" > <h4>จำนวนเงินรวมอุปการะทั้งหมด <?php echo number_format( $total_price ); ?> บาท</h4> </td> </tr> <tr> <td colspan= "8" style= "text-align: right;" > <a href= "order.php" type= "button" class = "btn btn-danger btn-lg" >ยืนยันทำรายการ</a> </td> </tr> </tbody> </table> </form> <?php } ?> |

คำแนะนำ และการใช้งาน
สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก
- ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
ความคิดเห็นที่
1
คำนวณได้สองแบบ จะใช้หรือ php หรือ js ก็ได้ หรือจะทำให้รองรับทั้งสองแบบก็ได้ แต่ก็จะต้องกำหนดเงื่อนไข
เพิ่มขึ้น ดูเนื้อหานี้เป็นแนวทาง
ประยุกต์รวมข้อมูลแถวในตาราง ด้วย jquery ร่วมกับ data attribute http://niik.in/727
https://www.ninenik.com/content.php?arti_id=727 via @ninenik
ส่วน php ก็ใช้ onchange ให้กับ select แล้วส่งค่าไป คำนวณใหม่

ขอบคุณทุกการสนับสนุน
![]()