2011-01-29 1 views
0

내가 선택한 상태를 유지하는 것으로 코드의 조각으로 테이블을 선택 출력을 결합하는 것을 시도하고있다 : 나는 모든 것을 시도선택된 상태 mysql을 배열

<select> 
<?php 
$desired_option = 'arsenal'; 
$arr = array('arsenal', 'aston villa', 'birmingham', 'blackpool', 'bolton'); 
for($i = 0; $i < count($arr); $i++) { 
$selected = ($arr[$i] == $desired_option) ? 'selected="selected"' : ''; 
echo "<option value=\"{$arr[$i]}\" {$selected}>{$arr[$i]}</option>"; 
} 
?> 
</select> 

<select id="teams" onchange="this.form.submit();" name="teamid"> 
<? 
include('db.php'); 
$getTeams = mysql_query("SELECT name, id FROM team") or die(mysql_error()); 
while ($teamsData = mysql_fetch_array($getTeams)) 
{ 
?>  
<option value="<? echo $teamsData['id']; ?>" ><? echo $teamsData['name']; ?></option> 
<? 
} 
?> 
</select> 

. 어떤 아이디어?

감사합니다 :)

답변

1
<select id="teams" onchange="this.form.submit();" name="teamid"> 
<? 
include('db.php'); 

$selected = 'team_to_be_selected'; 

$getTeams = mysql_query("SELECT name, id FROM team") or die(mysql_error()); 
while ($teamsData = mysql_fetch_array($getTeams)) 
{ 
?>  
<option value="<?php echo $teamsData['id']; ?>" <?php echo ($teamsData['name'] == $selected) ? 'selected="selected"' : ''; ?>><?php echo $teamData['name'];?></option> 
<? 
} 
?> 
</select>