|  | 
	                
  
    | 
	 
        ขอคำแนะนำเรื่อง option selected ทำยังไงให้แสดงข้อมูลเก่าก่อนหน้านั้นครับ     |  
    |  |  
 
	
		|  |  |  |  |  
		|  |  | 
          
            | ตัวอย่าง รายการรหัสที่ 66 มีประเภทสินค้าชื่อ "เครื่องดิ่ม" 
 
  
 พอกดไปหน้าแก้ไขข้อมูลสินค้า อยากให้ขึ้น show "เครื่องดื่ม" แล้วค่อยเลือกข้อมูลอื่น ที่เห็นในภาพคือ ประเภทสินค้ามี 6 รายการ
 แต่มันไป select ค้างไว้ที่ ประเภทสินค้าที่ 6 "ของใช้ส่วนตัว" แทนที่จะ show เป็นประเภทสินค้าที่ 1  "เครื่องดิ่ม" ต้องทำยังไงครับ
 
 
 
  
 
  
 code editproduct.php
 Code (PHP)
 
  <?php require_once('Connections/MyConnect.php'); ?>
<?php
if (!isset($_SESSION)) {
  session_start();
}
$MM_authorizedUsers = "";
$MM_donotCheckaccess = "true";
// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
  // For security, start by assuming the visitor is NOT authorized. 
  $isValid = False; 
  // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. 
  // Therefore, we know that a user is NOT logged in if that Session variable is blank. 
  if (!empty($UserName)) { 
    // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. 
    // Parse the strings into arrays. 
    $arrUsers = Explode(",", $strUsers); 
    $arrGroups = Explode(",", $strGroups); 
    if (in_array($UserName, $arrUsers)) { 
      $isValid = true; 
    } 
    // Or, you may restrict access to only certain users based on their username. 
    if (in_array($UserGroup, $arrGroups)) { 
      $isValid = true; 
    } 
    if (($strUsers == "") && true) { 
      $isValid = true; 
    } 
  } 
  return $isValid; 
}
$MM_restrictGoTo = "admin/index.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0) 
  $MM_referrer .= "?" . $_SERVER['QUERY_STRING'];
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo); 
  exit;
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }
  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
	$sql = "SELECT * FROM product WHERE (product_name='".$product_name."') AND product_id != $product_id";
$query = mysql_query($sql); 
if(mysql_num_rows($query) != 0){
echo "<script>alert('product_name Duplicate data Please check');history.back();</script>"; // ใส่ alert เลยครับ //ชื่อสินค้าซ้ำ กรุณาตรวจสอบใหม
exit();
}else{
  $updateSQL = sprintf("UPDATE product SET product_name=%s, cate_id=%s, unit_id=%s, product_balance=%s, product_price=%s,  product_qty=%s WHERE product_id=%s",
                       GetSQLValueString($_POST['product_name'], "text"),
                       GetSQLValueString($_POST['cate_id'], "text"),
                       GetSQLValueString($_POST['unit_id'], "text"),
                       GetSQLValueString($_POST['product_balance'], "int"),
                       GetSQLValueString($_POST['product_price'], "double"),
					   GetSQLValueString($_POST['product_qty'], "int"),
                       GetSQLValueString($_POST['product_id'], "int"));
  mysql_select_db($database_MyConnect, $MyConnect);
  $Result1 = mysql_query($updateSQL, $MyConnect) or die(mysql_error());
  $updateGoTo = "product.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}
}
$colname_Productset1 = "-1";
if (isset($_GET['product_id'])) {
  $colname_Productset1 = $_GET['product_id'];
}
mysql_select_db($database_MyConnect, $MyConnect);
$query_Productset1 = sprintf("SELECT * FROM product WHERE product_id = %s", GetSQLValueString($colname_Productset1, "int"));
$Productset1 = mysql_query($query_Productset1, $MyConnect) or die(mysql_error());
$row_Productset1 = mysql_fetch_assoc($Productset1);
$totalRows_Productset1 = mysql_num_rows($Productset1);$colname_Productset1 = "-1";
if (isset($_GET['product_id'])) {
  $colname_Productset1 = $_GET['product_id'];
}
mysql_select_db($database_MyConnect, $MyConnect);
$query_Productset1 = sprintf("SELECT * FROM product WHERE product_id = %s", GetSQLValueString($colname_Productset1, "int"));
$Productset1 = mysql_query($query_Productset1, $MyConnect) or die(mysql_error());
$row_Productset1 = mysql_fetch_assoc($Productset1);
$totalRows_Productset1 = mysql_num_rows($Productset1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>editproduct</title>
<style type="text/css">
#MenuBar1 {
	font-size: 230%;
	font-family: "Courier New", Courier, monospace;
}
body p {
	font-size: 18px;
	text-align: center;
	color: #000;
}
body h2 {
	text-align: center;
}
</style>
<script src="js/jquery.js" type="text/javascript"></script>
<link href="css/bootstrap.min.css" rel="stylesheet" type="text/css" />
</head>
<body class='container'>
<p><img src="shop.jpg" width="1117" height="149" />
</p>
<h3>ร้านเกสร
</h3>
<p>ระบบจัดการฐานข้อมูลสำหรับร้านค้าปลีก</p>
<div class="btn-group">
  <button class="btn"><a href="sales_detail.php">ขายสินค้า</a></button>
</div>
<div class="btn-group">
  <button class="btn">ข้อมูลพื้นฐาน</button>
  <button class="btn dropdown-toggle" data-toggle="dropdown">
    <span class="caret"></span>
  </button>  
  <ul class="dropdown-menu">
    <li><a href="Product.php">ข้อมูลสินค้า</a></li>
    <li><a href="Category.php">ข้อมูลประเภทสินค้า</a></li>
    <li><a href="Supplier.php">ข้อมูลผู้จัดจำหน่าย</a></li>
    <!-- dropdown menu links -->
  </ul> 
</div>
<div class="btn-group">
  <button class="btn"><a href="Unit.php">หน่วยสินค้า</a></button>
</div>
<div class="btn-group">
  <button class="btn"><a href="repayment">จัดการลูกหนี้</a></button>
</div>
<div class="btn-group">  
  <button class="btn">เพิ่มเติม</button>
  <button class="btn dropdown-toggle" data-toggle="dropdown">
    <span class="caret"></span>
  </button>  
  <ul class="dropdown-menu">
    <li><a href="customer.php">ข้อมูลลูกหนี้</a></li>
    <!-- dropdown menu links -->
  </ul>
</div>
  <div class="btn-group">
  <button class="btn"><a href="edit-profile.php">แก้ไขข้อมูลส่วนตัว</a></button>
</div>
  <div class="btn-group">
  <button class="btn"><a href="admin/index.php">ออกจากระบบ</a></button>
</div>
<p> </p>
<script language="javascript">
function fncSubmit()
{
	if(document.form1.product_name.value == "")
	{
		alert("กรุณากรอกชื่อสินค้า");
		document.form1.product_name.focus();
		return false;
	}
	if(document.getElementById('cate_id').value  == "0"  )
    {
        alert('กรุณาเลือกประเภท');
        return false;
    }
	if(document.getElementById('unit_id').value  == "0"  )
    {
        alert('กรุณาเลือกหน่วย');
        return false;
    }
	if(document.form1.product_balance.value == "")
	{
		alert("กรุณากรอกคงเหลือต่ำสุด");
		document.form1.product_balance.focus();
		return false;
	}
	if(document.form1.product_price.value == "")
	{
		alert("กรุณากรอกข้อมูลราคา");
		document.form1.product_price.focus();
		return false;
	}
	document.form1.submit();
}
</script>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1" onSubmit="JavaScript:return fncSubmit();">
  <table align="center">
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"> </td>
      <td>แก้ไขข้อมูลสินค้า</td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">รหัสสินค้า:</td>
      <td><?php echo $row_Productset1['product_id']; ?></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">ชื่อสินค้า:</td>
      <td><input type="text" name="product_name" value="<?php echo htmlentities($row_Productset1['product_name'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">ประเภทสินค้า:</td>
      <?php
mysql_connect("localhost", "root","0885272244") or die(mysql_error());
mysql_select_db("retail store") or die(mysql_error());
mysql_query("SET NAMES UTF8"); //Extension By DwThai.Com
$query = "SELECT * FROM  `category` ORDER BY  `cate_id` ASC LIMIT 0 , 30";
$result = mysql_query($query) or die(mysql_error()."[".$query."]");
?>
      <td><select name="cate_id" id="cate_id">
        <option value="0" >----เลือกประเภท----</option>    
       <?php   
while ($rs = mysql_fetch_array($result))
{
	?>
    <option value="<?=$rs['cate_id']; ?>" "<?=$rs['cate_name']; ?>" selected="selected"><?=$rs['cate_name']; ?></option>
 <?php } ?>
      </select></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">หน่วยสินค้า:</td>
      <?php
mysql_connect("localhost", "root","0885272244") or die(mysql_error());
mysql_select_db("retail store") or die(mysql_error());
mysql_query("SET NAMES UTF8"); //Extension By DwThai.Com
$query = "SELECT * FROM  `unit` ORDER BY  `unit_id` ASC LIMIT 0 , 30";
$result = mysql_query($query) or die(mysql_error()."[".$query."]");
?>
      <td><select name="unit_id" id="unit_id">
        <option value="0">----เลือกหน่วย----</option>
       
        <?php 
while ($rs = mysql_fetch_array($result))
{
    echo "<option value=".$rs['unit_id'].">".$rs['unit_name']."</option>";
}
?> 
      </select></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">คงเหลือต่ำสุด:</td>
      <td><input type="text" name="product_balance" value="<?php echo htmlentities($row_Productset1['product_balance'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">ราคา:</td>
      <td><input type="text" name="product_price" value="<?php echo htmlentities($row_Productset1['product_price'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">จำนวนสินค้า:</td>
      <td><input type="text" name="product_qty" value="<?php echo htmlentities($row_Productset1['product_qty'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"> </td>
      <td><input name="btn" type="submit" id="btn" value="ตกลง" />
      <input type="submit" name="btn2" id="btn2" value="ยกเลิก" /></td>
    </tr>
  </table>
  <input type="hidden" name="MM_update" value="form1" />
  <input type="hidden" name="product_id" value="<?php echo $row_Productset1['product_id']; ?>" />
</form>
<p> </p>
<script src="js/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
<?php
mysql_free_result($Productset1);
?>
 
 
 Tag : PHP, MySQL, HTML/CSS, JavaScript, jQuery, CakePHP
 
 
 |  
            |  |  
            | 
              
                |  |  |  |  
                |  | 
                    
                      | Date :
                          2016-07-03 02:02:04 | By :
                          suwidha | View :
                          3139 | Reply :
                          13 |  |  |  
                |  |  |  |  |  
            |  |  
		            |  |  
		|  |  |  |  |  
  
    | 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | <option value="ตัวแปร"  <?php echo (ตัวแปร == ตัวแปรforeign ? "SELECTED" : "")?> >ตัวแปรTEXT</option> 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2016-07-03 04:19:57 | By :
                            Chaidhanan |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ไม่จำเป็นต้องเป็น foreign key ครับ แต่ทำได้ก็ดี เพราะมันอ้างอิง เหมือน foreign key เดียวกัน 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2016-07-03 20:23:10 | By :
                            Chaidhanan |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | selected="selected"  อันนี้ไม่ต้องใส่ครับ เพราะเราใช้ <?php echo ตรวจสอบ แล้ว มันจะซ้ำซ้อน
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2016-07-03 21:05:17 | By :
                            Chaidhanan |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | rs[cate_id]  ไม่น่ามาเปรียบเทียบกับ product_id มั้้งครับ น่าจะเทียบกับ category_id อะไรทำนองนี้ที่อ้างอิงตัวเดียวกัน 
 จากบันทัด 091
 UPDATE product SET product_name=%s, cate_id=%s, unit_id=%s,
 และบันทัด126.
 $row_Productset1 = mysql_fetch_assoc($Productset1);
 
 ต้องเปรียบเทียบ $rs[cate_id] กับ $row_Productset1[cate_id]
 
 
 Code (PHP)
 
 <option value="<?=$rs['cate_id']; ?>"  
  <?=($rs['cate_id'] == $row_Productset1['cate_id']? "SELECTED" : "selected")?> 
> <?=$rs['cate_name']?></option>
 
 ปล
 mysql_select_db($database_MyConnect, $MyConnect);
 ทั้งไฟล์ ถ้า database เดียวกัน ใช้แค่บันทัดแรกบันทัดเดียวก็พอ มันใช้ได้จนจบโปรแกรม
 ไม่ต้อง เลือกใหม่
 
 |  
              | 
 ประวัติการแก้ไข
 2016-07-04 01:40:02
 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2016-07-04 01:36:12 | By :
                            Chaidhanan |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
 
        
          |  |  |  |  |  
          |  |  | 
            
              | Code (PHP) 
 <?php echo ($rs['cate_id'] == $row_Productset1['cate_id']? 'selected="selected"' : '')?>
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2016-07-04 15:46:55 | By :
                            fossil31 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ถ้าทำตามที่แนะนำยังไงก็ขึ้นแน่นอนครับ แต่เราต้องตรวจสอบเองด้วยว่า การรับค่ามาตรงกันหรือเปล่า เช่น รับค่ามาเป็น "ของใช้ส่วนตัว" แต่ค่าที่เอามาเปรียบเทียบมันมาไม่ตรงกับคำว่า "ของใช้ส่วนตัว" ค่า selected มันเลยไป selected ไม่ถูกตัว แต่ถ้าหากเราทำ foregnkey มันก็ง่ายกว่า เพราะมันมาเป็นตัวเลข 1==1 ก็จะตรวจสอบง่ายกว่าครับ
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2016-07-04 15:54:25 | By :
                            Dragons_first |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ผิดผลาดครับใช้ คห 9 เลยครับ copy มาลืมเปลี่ยน <?=($rs['cate_id'] == $row_Productset1['cate_id']? "SELECTED" : "selected")?>
 
 สองสี มีค่าเท่ากันเลย
 เปลี่ยนสีน้ำเงินเป็นค่าสตริงว่าง  ''
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2016-07-04 16:44:25 | By :
                            Chaidhanan |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | Code (PHP) 
 <?php echo ($rs['cate_id'] == $row_Productset1['cate_id']) ? '"selected"' : ''; ?>
 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2016-07-04 16:51:08 | By :
                            arm8957 |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  
 
        
          |  |  |  |  |  
          |  |  | 
            
              | ได้แล้วครับ ขอบคุณสำหรับทุกท่านมากครับ 
 |  
              | 
                
                  |  |  |  |  
                  |  | 
                      
                        | Date :
                            2016-07-04 20:43:04 | By :
                            suwidha |  |  |  
                  |  |  |  |  |  |  |  
          |  |  |  |  |  |  |