Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > PHP > PHP Forum > Chained Select แล้วให้แสดงข้อมูลที่เรา Select ออกมาเป็นtable ต้องยังไงต่อ



 

Chained Select แล้วให้แสดงข้อมูลที่เรา Select ออกมาเป็นtable ต้องยังไงต่อ

 



Topic : 120862



โพสกระทู้ ( 28 )
บทความ ( 0 )



สถานะออฟไลน์




ติดตรงจะทำยังไงต่อ ให้แสดงwhite ข้อมูลในdatabase ลงTable อะครับ

ทำตามแบบนี้มา http://www.yourinspirationweb.com/en/how-to-create-chained-select-with-php-and-jquery/

chainselect1

chainselect2

select.php
Code (PHP)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
			
			$("select#typeSub").attr("disabled","disabled");
			$("select#carType").change(function(){
			$("select#typeSub").attr("disabled","disabled");
			$("select#typeSub").html("<option>wait...</option>");
			var carType = $("select#carType option:selected").attr('value');
			
			$.post("select_type.php", {carType:carType}, function(data){
				$("select#typeSub").removeAttr("disabled");
       			$("select#typeSub").html(data);
              }); //post
      		}); //change
			
			$("select#yearStart").attr("disabled","disabled");
			$("select#typeSub").change(function(){
			$("select#yearStart").attr("disabled","disabled");
			$("select#yearStart").html("<option>wait...</option>");
			var typeSub = $("select#typeSub option:selected").attr('value');
			
			$.post("select_yearStart.php", {typeSub:typeSub}, function(data){
				$("select#yearStart").removeAttr("disabled");
       			$("select#yearStart").html(data);				
				}); //post
			}); //change
			
			$("select#yearEnd").attr("disabled","disabled");
			$("select#yearStart").change(function(){
			$("select#yearEnd").attr("disabled","disabled");
			$("select#yearEnd").html("<option>wait...</option>");
			var yearStart = $("select#yearStart option:selected").attr('value');
			
			$.post("select_yearEnd.php", {yearStart:yearStart}, function(data){
				$("select#yearEnd").removeAttr("disabled");
       			$("select#yearEnd").html(data);				
				}); //post
			}); //change
			
         	$("form#select_form").submit(function(){
				var carType = $("select#carType option:selected").attr('value');    
				var typeSub = $("select#typeSub option:selected").attr('value');
				var yearStart = $("select#yearStart option:selected").attr('value');
				var yearEnd = $("select#yearEnd option:selected").attr('value');
				if(carType !=0 && typeSub !=0 && yearStart >=0 && yearEnd >=0 )
				{
					var result1 = $("select#carType option:selected").html();
					var result2 = $("select#typeSub option:selected").html();
					var result3 = $("select#yearStart option:selected").html();
					var result4 = $("select#yearEnd option:selected").html();
					$("#result").html('Your choice: '+result1+' and '+result2+' and '+result3+' and '+result4);
				}
				else
				{
					$("#result").html("You must choose two options!");
				}
				return false;
            }); //submit
		}); //ready	
		
		
    </script>
    </head>
    <body>    
        <?php 
		include "select.class.php"; 
        include "css/connect.php";
		
		$result1 = intval($_GET['result1']);
		
        $sql="SELECT * FROM car WHERE cartype = '".$result1."' ";
        $result=mysql_query($sql);
		?>
    <form id="select_form">
            <select id="carType" style="width: 200px">
                <?php echo $opt->ShowMakes(); ?>
            </select>
        <br /><br />
        	<select id="typeSub" style="width: 200px">
            	<option value="0">All Model</option>
        	</select>
        <br /><br />
        	<select id="yearStart" style="width: 86px">
            	<option value="0">All Year</option>
            </select>
            &nbsp;to&nbsp;
        	<select id="yearEnd" style="width: 86px">
            	<option value="0">All Year</option>
            </select>
            <br /><br />
        <input type="submit" value="Search" />
        </form>
        <br /><br />
        <div id="result">
        
        <table border="1" cellpadding="0" cellspacing="0" id="datatable">
            <tr>
                <td width="80">Car</td>
                <td width="150">Model</td>
                <td width="50">Year</td>
            </tr>
            
            <?php 
				while($row = mysql_fetch_array($result)) {
			?>
            <tr>
                <td><?php echo $row['cartype'] ?></td>
                <td><?php echo $row['cartype_sub'] ?></td>
                <td><?php echo $row['caryear'] ?></td>
            </tr>
            <?php } //closing while
  			mysql_close($objConnect);//mysql connection close 
			?>
        </table>
    </div>
    </body>
</html>


select.class.php
Code (PHP)
<?php
class SelectList
{
    protected $conn;
 
        public function __construct()
        {
            $this->DbConnect();
        }
 
        protected function DbConnect()
        {
            include "db_config.php";
            $this->conn = mysql_connect($host,$user,$password) OR die("Unable to connect to the database");
            mysql_select_db($db,$this->conn) OR die("can not select the database $db");
            return TRUE;
        }
 
        public function ShowMakes()
        {
            $sql = "SELECT DISTINCT cartype FROM car ORDER BY cartype";
            $res = mysql_query($sql,$this->conn);
            $cartype = '<option value="0">All Makes</option>';
            while($row = mysql_fetch_array($res))
            {
                $cartype .= '<option value="' . $row['cartype'] . '">' . $row['cartype'] . '</option>';
            }
            return $cartype;
        }
 
        public function ShowModel()
        {
            $sql = "SELECT DISTINCT cartype_sub FROM car WHERE cartype='$_POST[carType]' ORDER BY cartype_sub";
            $res = mysql_query($sql,$this->conn);
            $typesub = '<option value="0">All Model</option>';
            while($row = mysql_fetch_array($res))
            {
                $typesub .= '<option value="' . $row['cartype_sub'] . '">' . $row['cartype_sub'] . '</option>';
            }
            return $typesub;
        }
		
		public function ShowYearStart()
        {
            $sql = "SELECT DISTINCT caryear FROM car WHERE cartype_sub='$_POST[typeSub]' ORDER BY caryear";
            $res = mysql_query($sql,$this->conn);
            $caryear = '<option value="0">All Year</option>';
            while($row = mysql_fetch_array($res))
            {
                $caryear .= '<option value="' . $row['caryear'] . '">' . $row['caryear'] . '</option>';
            }
            return $caryear;
        }
		
		public function ShowYearEnd()
        {
            $sql = "SELECT DISTINCT caryear FROM car WHERE cartype_sub='$_POST[typesub]' AND caryear>='$_POST[yearStart]' ORDER BY caryear";
            $res = mysql_query($sql,$this->conn);
            $caryear = '<option value="0">All Year</option>';
            while($row = mysql_fetch_array($res))
            {
                $caryear .= '<option value="' . $row['caryear'] . '">' . $row['caryear'] . '</option>';
            }
            return $caryear;
        }
}
 
$opt = new SelectList();


?>




Tag : PHP, HTML/CSS, JavaScript, jQuery, CakePHP, JAVA









ประวัติการแก้ไข
2016-01-13 11:47:37
2016-01-13 11:48:21
2016-01-13 11:59:56
2016-01-13 12:03:03
2016-01-13 12:03:35
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2016-01-13 11:46:10 By : best5566 View : 1216 Reply : 2
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

Code (PHP)
       <table border="1" cellpadding="0" cellspacing="0" id="datatable">
            <tr>
                <td width="80">Car</td>
                <td width="150">Model</td>
                <td width="50">Year</td>
            </tr>
            
            <?php 
				while($row = mysql_fetch_array($result)) {
			?>
            <tr>
                <td><?php echo $row['cartype'] ?></td>
                <td><?php echo $row['cartype_sub'] ?></td>
                <td><?php echo $row['caryear'] ?></td>
            </tr>
            <?php } //closing while
  			mysql_close($objConnect);//mysql connection close 
			?>
        </table>


มันก็ Loop แล้วนี่ครับ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-01-13 13:30:32 By : mr.win
 


 

No. 2



โพสกระทู้ ( 28 )
บทความ ( 0 )



สถานะออฟไลน์


ตอบความคิดเห็นที่ : 1 เขียนโดย : mr.win เมื่อวันที่ 2016-01-13 13:30:32
รายละเอียดของการตอบ ::
อันนี้ผมเพิม่เข้าไปเองอะครับ ใส่เตรียมไว้รอบรับข้อมูลมาวนloop แต่มันยังไม่โชอะครับ ที่ขึ้นแบบรูปที่1

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-01-13 14:38:32 By : best5566
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : Chained Select แล้วให้แสดงข้อมูลที่เรา Select ออกมาเป็นtable ต้องยังไงต่อ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 02
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่