|
 |
|
ขอคำแนะนำเรื่องการ Select ข้อมูลลงใน dropdown menu ครับผม |
|
 |
|
|
 |
 |
|
อธิบายฐานข้อมูลก่อนนะครับ
ผมต้องการ Select ข้อมูลรายการอาหาร โดยรายการอาหารจะแบ่งตามเซทราคา
เช่น 200,250,300,350 บาท
และเมื่อเลือกเซทราคาอาหาร เช่น 200 บาท จะมีหัวข้อ 6-7 หัวข้อ
ในแต่ละหัวข้อจะเลือกรายการอาหารได้ 1 อย่าง ดังรูปภาพด้านล่าง

#ปัญหาคือเมื่อเลือกเซทราคา 200 บาท ผมทำให้มันมีหัวข้อให้เลือกได้แค่ 1 รายการ
ซึ่งจริงๆต้องมี 6-7 รายการ และเมื่อเลือกหัวข้ออาหาร เช่นหัวข้อ 1 จะต้องดึงข้อมูลเมนูอาหารจากเซทราคา 200 บาทเท่านั้น
แต่ตอนนี้มันดึงข้อมูลมาทั้งหมดเลย ต้องทำยังไงครับ ขอคำแนะนำด้วยนะครับ
//index.php
<?php
function ListPrice()
{
require_once("connection.php");
$output = '';
$query = "SELECT ListPrice FROM listmenu GROUP BY ListPrice ORDER BY ListPrice ASC";
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_array($result))
{
$output .= '<option value="'.number_format($row["ListPrice"]).'">'.number_format($row["ListPrice"]).' บาท ต่อท่าน </option>';
}
return $output;
}
?>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<br /><br />
<div class="container" style="width:600px;">
<select name="ListPriceSel" id="ListPriceSel" class="form-control action">
<option value="">-- เลือกราคาอาหาร --</option>
<?php echo ListPrice(); ?>
</select>
<br />
<select name="ListTopicSel" id="ListTopicSel" class="form-control action">
<option value="">-- เลือกหัวข้ออาหาร --</option>
</select>
<br />
<select name="MenuName" id="MenuName" class="form-control">
<option value="">-- เลือกรายการอาหาร --</option>
</select>
</div>
</body>
</html>
<script>
$(document).ready(function(){
$('.action').change(function(){
if($(this).val() != '')
{
var action = $(this).attr("id");
var query = $(this).val();
var result = '';
if(action == "ListPriceSel")
{
result = 'ListTopicSel';
}
else
{
result = 'MenuName';
}
$.ajax({
url:"FetchSelect.php",
method:"POST",
data:{action:action, query:query},
success:function(data){
$('#'+result).html(data);
}
})
}
});
});
</script>
//FetchSelect.php
[php]<?php
if(isset($_POST["action"]))
{
require_once("connection.php");
$output = '';
if($_POST["action"] == "ListPriceSel")
{
$query = "SELECT ListTopic FROM listmenu WHERE ListPrice = '".$_POST["query"]."' GROUP BY ListTopic";
$result = mysqli_query($conn, $query);
$output .= '<option value="">-- เลือกหัวข้ออาหาร --</option>';
while($row = mysqli_fetch_array($result))
{
$output .= '<option value="'.$row["ListTopic"].'">'.$row["ListTopic"].'</option>';
}
}
if($_POST["action"] == "ListTopicSel")
{
$query = "SELECT MenuName FROM listmenu WHERE ListTopic = '".$_POST["query"]."'";
$result = mysqli_query($conn, $query);
$output .= '<option value="">-- เลือกรายการอาหาร --</option>';
while($row = mysqli_fetch_array($result))
{
$output .= '<option value="'.$row["MenuName"].'">'.$row["MenuName"].'</option>';
}
}
echo $output;
}
?>
[/php]
รูปภาพตัวอย่างฐานข้อมูล

Tag : PHP

|
|
 |
 |
 |
 |
Date :
2018-05-23 16:41:20 |
By :
RabbitTie |
View :
718 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่มีใครตอบเลยฮะ
|
 |
 |
 |
 |
Date :
2018-05-24 08:32:02 |
By :
RabbitTie |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
https://www.thaicreate.com/php/forum/083883.html
|
 |
 |
 |
 |
Date :
2018-05-24 08:45:37 |
By :
RabbitTie-II |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
//ก็ทำให้มันมีเงื่อนไข 2 ตัว
$query = "SELECT MenuName FROM listmenu WHERE ListTopic = '".$_POST["query"]."' and ListPrice='".$_POST["value"]."'";
ก็เอาเงื่อนไข ราคา 200 มาคิดกับ 1 เซ็ต
ช่องสุดท้ายก็น่าจะได้ 3 รายการ มั่งนะถ้าผมดูจากฐานข้อมูล
่
|
ประวัติการแก้ไข 2018-05-24 10:26:27
 |
 |
 |
 |
Date :
2018-05-24 10:20:53 |
By :
nobetaking |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 05
|