我需要在$ _GET []给出的某些条件下选择“选中”的解决方案。 这个想法是。
<?
$sql_id="select * from stock order by id asc";
$result_id=mysql_db_query($dbname,$sql_id);
while($rec_id=mysql_fetch-array($result_id)){
$_id=$rec_id['id'];
$_title=$rec_id['title'];
if($_GET['id']==$_id){
echo "<option value=\"$_id\" selected>$_title</option>";
}else{//I need this option selected where id='2'
echo "<option value=\"$_id\">$_title</option>";
}
?>
问题是:我还需要空($ _ GET ['id'])= 2。那么条件就是
答案 0 :(得分:0)
不确定我是否理解正确,但你为什么不尝试这样的事情:
if($_GET['id']==$_id){
echo "<option value=\"$_id\" selected>$_title</option>";
}
else if (($_GET['id']=="") && ($_id==2)){//I need this option selected where id='2'
echo "<option value=\"$_id\">$_title</option>";
}
答案 1 :(得分:0)
<?php
$query = "SELECT * from `".$dbname."`.`stock` ORDER BY `id` ASC";
$result_id = mysql_query($query);
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
while($rec_id = mysql_fetch_array($result_id)){
$sel = '';
if($rec_id['id'] == $id){
$sel = " selected='selected'";
}
if(!$id && $rec_id['id'] == 2){
$sel = " selected='selected'";
}
echo "<option value='".$rec_id['id']."'".$sel.">".$rec_id['title']."</option>";
}
?>
答案 2 :(得分:0)
我现在知道了......
<?
//let's test the $_GET['id'] value first
if(!$_GET['id']){
$_id="2";//if no $_GET['id'] then $_id==2
}elseif(isset($_GET['id'])){
$_id=$_GET['id'];
}
$sql_id="select * from stock order by id asc";
$result_id=mysql_db_query($dbname,$sql_id);
while($rec_id=mysql_fetch-array($result_id)){
$_id=$rec_id['id'];
$_title=$rec_id['title'];
if($_GET['id']==$_id){
echo "<option value=\"$_id\" selected>$_title</option>";
}else{//I need this option selected where id='2'
echo "<option value=\"$_id\">$_title</option>";
}
?>
问题解决了!非常感谢你的帮助!