Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在填充的PHP下拉列表中选择匹配的值_Php_Mysql_Wordpress - Fatal编程技术网

在填充的PHP下拉列表中选择匹配的值

在填充的PHP下拉列表中选择匹配的值,php,mysql,wordpress,Php,Mysql,Wordpress,我已经通过MySQL中的php创建了填充的下拉列表。代码#1 然后我通过代码#3在页面中显示它 $theformbody.=”; $theformbody.=_u(“区域”、“AWPCP”); $theformbody.=“$allreas”; 我想将下面的代码添加到代码#1中 $output.=''。条纹斜杠($savedarea)。""; 因此,在启动函数$allareas=create_dropdown_areanames($adcontact_country)时如果$adcontac

我已经通过MySQL中的php创建了填充的下拉列表。代码#1

然后我通过代码#3在页面中显示它

$theformbody.=”;
$theformbody.=_u(“区域”、“AWPCP”);
$theformbody.=“$allreas

”;
我想将下面的代码添加到代码#1中

$output.=''。条纹斜杠($savedarea)。"";

因此,在启动函数$allareas=create_dropdown_areanames($adcontact_country)时如果$adcontact_country变量与数据库字符串匹配,则它将是下拉列表中选择的变量,否则下拉列表将从上到下生成。提前感谢

只需在foreach中添加一个三元运算符即可

 foreach ($savedareaslist as $savedarea) {
        $output .= "<option ".($savedarea==$adcontact_country?'selected="selected" ':'')."value=\"" . esc_attr($savedarea) . "\">" . stripslashes($savedarea) . "</option>";
    }

我不太清楚您在问什么,但我认为您首先需要向函数
create\u dropdown\u areanames()
中添加一个参数,然后您可以在
foreach($savedareaslist as$savedarea)
中检查它,并输出所选选项(如果匹配)?感谢@StrikeForceZero的帮助。这正是我所需要的,迈克尔给了我一个简单的方法:)。非常感谢你,迈克尔。在过去的7个小时里,我一直在努力寻找答案。我知道这应该很简单,但对php的了解太差,这几乎不可能解决。非常感谢你的帮助。
        $allareas = create_dropdown_areanames($adcontact_country);
$theformbody.="<select name=\"adarea\" onchange=\"changeareameo();\" id='add_new_ad_area'><option value=\"\">";
$theformbody.=__("Area","AWPCP");
$theformbody.="</option>$allareas</select></p>";
$output .= "<option selected='selected' value=\"" . esc_attr($savedarea) . "\">" . stripslashes($savedarea) . "</option>";
 foreach ($savedareaslist as $savedarea) {
        $output .= "<option ".($savedarea==$adcontact_country?'selected="selected" ':'')."value=\"" . esc_attr($savedarea) . "\">" . stripslashes($savedarea) . "</option>";
    }
function create_dropdown_areanames($adcontact_country=null) {