ตัวอย่างฟังก์ชั่นต่อไปนี้ ใช้รูปแบบการเชื่อมต่อฐานข้อมูลด้วย mysqli
เป็นการฟังก์ชั่นสำหรับสร้าง select tag ที่สามารถดึงข้อมูลมาแสดง
ในรายการ option รองรับกรณีการแก้ไข
หากต้องการใช้งานกับ mysql สามารถนำไปดัดแปลงเพิ่มเติมได้
หรือสามารถประยุกต์กรณีอื่นๆ ได้ตามต้องการ
ต่อไปตัวอยางกรณีการใช้งานกับฐานข้อมูล
เราจะใช้วิธีการเชื่อมต่อแบบ mysqli แทน mysql ธรรมดา
เพื่อรองรับในอนาคต
ดาวน์โหลดไฟล์ พร้อมใช้ ชื่อ db_connect.php ได้ที่
ตัวอย่าง
ฟังก์ชั่น
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 | <?php function SelectInput( $sql =null, $se_Name = "select1" , $se_Id =null, $se_Class =null, $se_Style =null, $se_text = "" , $se_Select =null, $filed_option =null, $field_value =null ){ global $mysqli ; $se_Id =( $se_Id ==null)? $se_Name : $se_Id ; $se_text =( $se_text ==null)? "กรุณาเลือกรายการ" : $se_text ; $tagChoose = '<option value="">' . $se_text . '</option>' ; $tagOption =null; if ( $sql !=null){ $rs = $mysqli ->query( $sql ); $total = $rs ->num_rows; while ( $data = $rs ->fetch_assoc()){ $tagSelected =null; $filed_optionText = $data [ $filed_option ]; $field_valueText =( $field_value ==null)? $filed_optionText : $data [ $field_value ]; if ( $se_Select !=null && $field_valueText == $se_Select ){ $tagSelected = " selected" ; } $tagOption .= '<option value="' . $field_valueText .'" '.$tagSelected.' > '.$filed_optionText.' </option>'; } } $tagSelect = <<<TAG <select name= "$se_Name" id= "$se_Id" class = "$se_Class" style= "$se_Style" > $tagChoose $tagOption </select> TAG; // บรรทัดด้านบนนี้ที่ (TAG;) นี้ต้องชิดซ้ายสุด ห้ามมีช่องว่างทั้งหน้าและหลัง echo $tagSelect ; } ?> |
การใช้งาน parameter
1 2 3 4 5 6 7 8 9 10 11 | SelectInput( "คำสั่ง sql" , "ชื่อ select" , "id ถ้ามี" , "class ถ้ามี" , "style อื่นๆ ถ้ามี" , "ข้อความแจ้งให้เลือก ถ้ามี" , "ค่ารายการที่ถูกเลือก กรณีใช้แก้ไข" , "ข้อความของ option" , "ค่าของ option" } |
โค้ดและตัวอย่างทั้งหมด
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 | <?php include ( "db_connect.php" ); // เรียกใช้ไฟล์ ตั้งค่า และฟังก์ชั่น เกี่ยวกับฐานข้อมูล $mysqli = connect(); // สร้าง ตัวแปร mysqli instance สำหรับเรียกใช้งานฐานข้อมูล ?> <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <title>Document</title> <link rel= "stylesheet" href= "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" > </head> <body> <br> <br> <br> <?php function SelectInput( $sql =null, $se_Name = "select1" , $se_Id =null, $se_Class =null, $se_Style =null, $se_text = "" , $se_Select =null, $filed_option =null, $field_value =null ){ global $mysqli ; $se_Id =( $se_Id ==null)? $se_Name : $se_Id ; $se_text =( $se_text ==null)? "กรุณาเลือกรายการ" : $se_text ; $tagChoose = '<option value="">' . $se_text . '</option>' ; $tagOption =null; if ( $sql !=null){ $rs = $mysqli ->query( $sql ); $total = $rs ->num_rows; while ( $data = $rs ->fetch_assoc()){ $tagSelected =null; $filed_optionText = $data [ $filed_option ]; $field_valueText =( $field_value ==null)? $filed_optionText : $data [ $field_value ]; if ( $se_Select !=null && $field_valueText == $se_Select ){ $tagSelected = " selected" ; } $tagOption .= '<option value="' . $field_valueText .'" '.$tagSelected.' > '.$filed_optionText.' </option>'; } } $tagSelect = <<<TAG <select name= "$se_Name" id= "$se_Id" class = "$se_Class" style= "$se_Style" > $tagChoose $tagOption </select> TAG; // บรรทัดด้านบนนี้ที่ (TAG;) นี้ต้องชิดซ้ายสุด ห้ามมีช่องว่างทั้งหน้าและหลัง echo $tagSelect ; } ?> <div style= "margin:auto;width:80%;" > <?php SelectInput(" SELECT * FROM tbl_sale ORDER BY sale_date ", "product_name" ,null,null,null, "เลือกรายการสินค้า" ,null, "sale_product" , "sale_product" ); ?> <br><br> <?php SelectInput(" SELECT * FROM tbl_sale ORDER BY sale_date "," product_name ",null,null," width:250px;", "เลือกรายการสินค้า" , "Product C" , "sale_product" ,null ); ?> <br><br> <?php $_GET [ 'data_select' ]= "Product F" ; SelectInput(" SELECT * FROM tbl_sale ORDER BY sale_date "," product_name",null,null,null, null, $_GET [ 'data_select' ], "sale_product" ,null ); ?> </div> </body> </html> |