如何在不使用数据库的情况下选择国家/地区下拉选择表单项?
我有这个 PHP 代码,我用它来选择国家/地区的下拉表单 我想消除这个额外的 mysql 查询,只是将输出存储在页面上的代码中 但是,如果我不使用查询来获取数据,我不知道如何选择用户国家/地区 请建议
<select name="country" style="width:180px;" onChange="do_get_rest_popup(this.value)" o>
<?PHP
$sql="SELECT * FROM users_countries ORDER BY country";
$result_country = executequery($sql);
while($line_country = mysql_fetch_array($result_country)){
$db_country_id = $line_country['id'];
$country_name = $line_country['country'];
?>
<option value="<?=$db_country_id?>"<? if($line_member['country']==$db_country_id){ echo " SELECTED";} ?>><?=$country_name?></option>
<?
}
?>
</select>
有关页面上输出的代码,我已经为这篇文章减少了国家/地区的数量
<select name="country" style="width:180px;" onChange="do_get_rest_popup(this.value)" o>
<option value="217">Turkmenistan</option>
<option value="218">Turks and Caicos Islands</option>
<option value="219">Tuvalu</option>
<option value="220">Uganda</option>
<option value="221">Ukraine</option>
<option value="222">United Arab Emirates</option>
<option value="223">United Kingdom (Great Britain)</option>
<option value="224" SELECTED>United States</option>
</select>
I have this PHP code that I use to make a dropdown form selection of country's
I would like to eliminate this extra mysql query though and just store the output like in code to on the page
However I am lost on how I would have the users country SELECTED If I do not use the query to get the data
Please advice
<select name="country" style="width:180px;" onChange="do_get_rest_popup(this.value)" o>
<?PHP
$sql="SELECT * FROM users_countries ORDER BY country";
$result_country = executequery($sql);
while($line_country = mysql_fetch_array($result_country)){
$db_country_id = $line_country['id'];
$country_name = $line_country['country'];
?>
<option value="<?=$db_country_id?>"<? if($line_member['country']==$db_country_id){ echo " SELECTED";} ?>><?=$country_name?></option>
<?
}
?>
</select>
Code about outputs this on page, I have strunk the quantity of countries down for this post
<select name="country" style="width:180px;" onChange="do_get_rest_popup(this.value)" o>
<option value="217">Turkmenistan</option>
<option value="218">Turks and Caicos Islands</option>
<option value="219">Tuvalu</option>
<option value="220">Uganda</option>
<option value="221">Ukraine</option>
<option value="222">United Arab Emirates</option>
<option value="223">United Kingdom (Great Britain)</option>
<option value="224" SELECTED>United States</option>
</select>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以缓冲静态 html 并为所选值执行简单的字符串替换。
1) 将国家/地区 HTML 列表保存到
countries.html
2) 在加载时,将
countries.html
读入变量并进行字符串替换:总体而言内存效率不高,但它确实节省了数据库命中和处理时间。
You could buffer the static html and do a simple string replace for the selected value.
1) Save the country HTML list into
countries.html
2) At load time, read
countries.html
into a variable and do a string replace:Not very memory efficient overall, but it does save the database hit and processing time.
您可以:
通过上述两个选项,您将获得一个数组,您可以按照与现在正在执行的操作类似的方式循环遍历该数组,以生成 html。
You could:
With both the above options, you then have an array which you can loop over in a similar way to what you are doing now in order to generate the html.
像这样的事情怎么样?
编辑:我没有测试这个,但你应该明白
How about something like this?
EDIT: I did not test this but you should get the idea