|
 |
|
รบกวนทีครับ "การนำ enum จาก mysql มาโชว์ใน dropdown พร้อม selected ค่าที่มีอยู่แล้ว" |
|
 |
|
|
 |
 |
|
รบกวนช่วยทีครับ
ที่ทำไปแล้ว คือ ..
- สามารถเรียกข้อมูลประเภท ENUM จาก ฐานข้อมูล MySQL ออกมาอยู่ในรูปแบบ Dropdown List ได้แล้วครับ 
ส่วนต่อไปคือที่ต้องการให้ช่วยครับ
- ทำอย่างไรให้ Dropdown List มัน selected ค่าที่มีอยู่ในฐานข้อมูลของ User คนนั้นๆ อยู่แล้ว ???
****************************************************************************
เกี่ยวกับที่จอดรถครับ ว่า User มีหรือไม่มี
ใน ENUM มี 3 ตัวเลือกครับ คือ "ไม่มี", "ข้างทาง", "ลานจอดรถ"
แล้ว User คนนี้มีค่า As Defined : ลานจอดรถ อยู่ในฐานข้อมูลครับ
แต่พอเรียกเพจขึ้นมา ช่อง Dropdown List มันจะไปอยู่ที่ ไม่มี (คือตัวแปรตัวแรก) เองโดยอัตโนมัติอ่ะครับ
รบกวนช่วยทีครับ
****************************************************************************
โดยเบื้องต้นแล้วมี CODE ดังนี้ครับ
Code (PHP)
<?php
$table = "user";
$column = "user_parking";
$sql = "SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '$table' AND COLUMN_NAME = '$column'";
$result = mysql_query($sql) or die(mysql_error());
$array = mysql_fetch_array($result);
$enumList = explode(",", str_replace("'", "", substr($array['COLUMN_TYPE'], 5, (strlen($array['COLUMN_TYPE'])-6))));
?>
<label for="parking">ที่จอดรถ :</label>
<select name="parking" id="parking" class="form-control">
<?php foreach ($enumList as $value) : ?>
<option value="<?php echo $value; ?>"><?php echo $value; ?></option>
<?php endforeach; ?>
</select>
ขอบคุณครับ
Tag : PHP, MySQL, HTML/CSS

|
|
 |
 |
 |
 |
Date :
2016-04-21 16:08:24 |
By :
obstinatez |
View :
3044 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
<select name="lmName1">
<option value=""><-- Please Select Item --></option>
<?php
$strSQL = "SELECT * FROM customer ORDER BY CustomerID ASC";
$objQuery = mysql_query($strSQL);
while($objResuut = mysql_fetch_array($objQuery))
{
if($strDefault == $objResuut["CustomerID"])
{
$sel = "selected";
}
else
{
$sel = "";
}
?>
<option value="<?php echo $objResuut["CustomerID"];?>" <?php echo $sel;?>><?php echo $objResuut["CustomerID"]." - ".$objResuut["Name"];?></option>
<?php
}
?>
</select>
|
 |
 |
 |
 |
Date :
2016-04-21 16:16:35 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับท่าน Admin แต่ยังไม่ได้ครับ
ผมลองมาหลายตัวแปรแล้วครับ ตรงที่ผม ###### ไว้ครับ หรือผมพลาดตรงไหนไปครับ
ชี้แนะด้วยครับ
Code (PHP)
<?php
//ข้างบนเคย fetch_assoc เป็น $row["user_parking"];
//echo $row["user_parking"]; ออกมาแล้วได้ค่าที่อยู่ในฐานข้อมูล
.........................................
....................................
..........................
$table = "user";
$column = "user_parking";
$strDefault = $row["user_parking"];
$sql = "SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '$table' AND COLUMN_NAME = '$column'";
$result = mysql_query($sql) or die(mysql_error());
$array = mysql_fetch_array($result);
$enumList = explode(",", str_replace("'", "", substr($array['COLUMN_TYPE'], 5, (strlen($array['COLUMN_TYPE'])-6))));
?>
<label for="parking">ที่จอดรถ :</label>
<select name="parking" id="parking" class="form-control">
<?php
foreach ($enumList as $value) :
if ($strDefault == #######) {
$sel = "selected";
} else {
$sel = "";
}
<option value="<?php echo $value; ?>" <?php echo $sel; ?>><?php echo $value; ?></option>
<?php endforeach; ?>
</select>
|
 |
 |
 |
 |
Date :
2016-04-21 16:39:53 |
By :
obstinatez |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 02
|