开发者

set class='selected ' in more than one option tag (list/menu)

开发者 https://www.devze.com 2023-03-22 11:20 出处:网络
Sorry, I want to ask a newb开发者_运维百科ie question :) I have PHP scripts like this : <select id=\"member\">

Sorry, I want to ask a newb开发者_运维百科ie question :)

I have PHP scripts like this :

<select id="member">
 <?php
  $sql   = " select id,name from member order by name ";
  $query = mysql_query($sql);
  while($row=mysql_fetch_array($query))
  {             
 ?>
  <option value="<?php echo $row["id"]; ?>"><?php echo $row["name"]; ?></option>
 <?php
  }
 ?>                
</select>

then the result will be generating html like this

<select id="member">
 <option value="1">Jon Skeet</option>
 <option value="2">Marc Gravell</option>
 <option value="3">Darin Dimitrov</option>
</select>

my question is, actually I have 2 tables in mysql, first is member table and second member_invited table.

in member table:

id | name

---------------

00 | Jon Skeet

01 | Marc Gravell

02 | Darin Dimitrov

then in member_invited table:

id | name

---------------

00 | Jon Skeet

02 | Darin Dimitrov

within this two table I want to make my list like this:

(look at class="selected" is in option tag that member has invited)

<select id="member">
 <option value="1">Jon Skeet</option>
 <option value="2" class="selected">Marc Gravell</option>
 <option value="3" class="selected">Darin Dimitrov</option>
</select>

please feel free to using javascript or jquery,, or maybe I can solve it just by using PHP ?

many thanks :)


you have to do it in MySQL with a join to member_invited table and then extend your while loop regarding is_invited flag from resultset

<?php
  $sql = "SELECT x.id,x.name,IFNULL(y.id,0) AS is_invited FROM member x LEFT JOIN member_invited y ON x.id=y.id ORDER BY x.name";
  $query = mysql_query($sql);
  while($row=mysql_fetch_array($query))
  {             
?>
<option value="<?php echo $row["id"]; ?>"<?php if ($row['is_invited']>0) { echo " class=\"selected\""; } ?>><?php echo $row["name"]; ?></option>
<?php
  }
?>


Try something like this

$("#member option").each(function(){
    var $this = $(this);
   if($this.val() == "2" || $this.val() == "3"){
      $this.addClass("selected");
   }
});


You probably want to use something like this to enable multiple selection :

http://www.w3schools.com/tags/att_select_multiple.asp

And it's more clever to do what you want server-side...

0

精彩评论

暂无评论...
验证码 换一张
取 消