ThaiSEOBoard.com

พัฒนาเว็บไซต์ => Programming => ข้อความที่เริ่มโดย: TopPopWeb ที่ 21 เมษายน 2011, 17:24:28



หัวข้อ: ขอความช่วยเหลือเกี่ยวกับโค้ด php ครับ
เริ่มหัวข้อโดย: TopPopWeb ที่ 21 เมษายน 2011, 17:24:28
สวัสดีครับ พี่ๆคนไหนที่เก่ง php ช่วยดูโค้ดให้หน่อยครับ ข้างล่างคือโค้ดที่ใช้อยู่ปัจจุบันแต่ว่าผมอยากเปลี่ยนจาก drop down list ให้เป็น radio button น่ะครับ แต่ผมไม่มีความรู้ด้านสคริปเลย ไม่ทราบว่าต้องแก้ไขที่บรรทัดไหนบ้างครับ หาใน google ลองแล้วก็ทำไม่ได้ครับ ขอบคุณล่วงหน้าครับ

โค๊ด:
      <select name="sub_id" id="sub_id">
      <option value=""></option>
      <?
if($cat_id!="") {
$SQL = "select * from tb_sub_category where cat_id=$cat_id order by name";
} else {
$SQL = "select * from tb_sub_category where cat_id=0 order by name";
}

$result1 = mysql_query($SQL) or die(error_msg(mysql_error(),$SQL));
$number = mysql_num_rows($result1);
for($i=1;$i<=$number;$i++) {
$tmp1 = mysql_fetch_array($result1);
?>
        <option value="<?=$tmp1["sub_id"]?>" <?=($tmp1["sub_id"]==$tmp["sub_id"])?"selected":""?>>
        <?=$tmp1["name"]?>
        </option>
        <? } ?>
        </select>


หัวข้อ: Re: ขอความช่วยเหลือเกี่ยวกับโค้ด php ครับ
เริ่มหัวข้อโดย: smapan ที่ 21 เมษายน 2011, 17:28:45
รอดูด้วยคน  :-[


หัวข้อ: Re: ขอความช่วยเหลือเกี่ยวกับโค้ด php ครับ
เริ่มหัวข้อโดย: lucationjj ที่ 21 เมษายน 2011, 17:34:09
<option value="<?=$tmp1["sub_id"]?>" <?=($tmp1["sub_id"]==$tmp["sub_id"])?"selected":""?>>
        <?=$tmp1["name"]?>
        </option>

เปลี่ยนตรงนี้ มั่ง  :-[


หัวข้อ: Re: ขอความช่วยเหลือเกี่ยวกับโค้ด php ครับ
เริ่มหัวข้อโดย: TopPopWeb ที่ 21 เมษายน 2011, 17:42:14
<option value="<?=$tmp1["sub_id"]?>" <?=($tmp1["sub_id"]==$tmp["sub_id"])?"selected":""?>>
        <?=$tmp1["name"]?>
        </option>

เปลี่ยนตรงนี้ มั่ง  :-[

ขอบคุณครับที่แนะนำ แต่ยังไม่เข้าใจ ช่วยอธิบายอีกหน่อยได้ไหมครับว่าต้องเพิ่มหรือลบอะไรออกบ้าง ขอบคุณครับ


หัวข้อ: Re: ขอความช่วยเหลือเกี่ยวกับโค้ด php ครับ
เริ่มหัวข้อโดย: ultrasad ที่ 21 เมษายน 2011, 17:45:18
โค๊ด:

<?php

if(
$cat_id!="") { 
$SQL "select * from tb_sub_category where cat_id=$cat_id order by name";
} else {
$SQL "select * from tb_sub_category where cat_id=0 order by name";
}

$result1 mysql_query($SQL) or die(error_msg(mysql_error(),$SQL));
$number mysql_num_rows($result1);
for($i=1;$i<=$number;$i++) { 
$tmp1 mysql_fetch_array($result1);
?>


<input type='radio' name='<?=$tmp1["name"]?>' value='<?=$tmp1["sub_id"]?>' <?=($tmp1["sub_id"]==$tmp["sub_id"])?"checked":""?> > <?=$tmp1["name"]?> <br />

<?php ?>


ลองดูนะครับ ไม่ได้เช็ค  :P


หัวข้อ: Re: ขอความช่วยเหลือเกี่ยวกับโค้ด php ครับ
เริ่มหัวข้อโดย: compsci50 ที่ 21 เมษายน 2011, 17:46:55
<?
if($cat_id!="") {
      $SQL = "select * from tb_sub_category where cat_id=$cat_id order by name";
   } else {
      $SQL = "select * from tb_sub_category where cat_id=0 order by name";
   }

   $result1 = mysql_query($SQL) or die(error_msg(mysql_error(),$SQL));
   $number = mysql_num_rows($result1);
   for($i=1;$i<=$number;$i++) {
   $tmp1 = mysql_fetch_array($result1);
   $check="";
   if($tmp1["sub_id"]==$tmp["sub_id"]) $check="checked='checked'";
   ?>
   <input name="sub_id" type="radio" value="<?=$tmp1["sub_id"]?>" <? echo $check; ?> />"<? echo $tmp1["name"]; ?>
   <? } ?>
?>


หัวข้อ: Re: ขอความช่วยเหลือเกี่ยวกับโค้ด php ครับ
เริ่มหัวข้อโดย: TopPopWeb ที่ 21 เมษายน 2011, 18:03:34
เย้ ทำได้แล้วครับโดยใช้สคริปของคุณ compsci50
ส่วนคุณ ultrasad สคริปยัง error อยู่ครับ แต่ยังไงต้องขอขอบคุณที่เข้ามาช่วยตอบนะครับ

ขอบคุณทุกท่านครับ  :wanwan017: