如何在不使用数据库的情况下选择国家/地区下拉选择表单项?

发布于 2024-07-28 15:25:38 字数 1337 浏览 4 评论 0原文

我有这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

东京女 2024-08-04 15:26:08

您可以缓冲静态 html 并为所选值执行简单的字符串替换。

1) 将国家/地区 HTML 列表保存到 countries.html

2) 在加载时,将 countries.html 读入变量并进行字符串替换:

$countries = file_get_contents('countries.html');    
echo str_replace('value="'.$userCountry.'"','value="'.$userCountry.'" SELECTED',$countries);

总体而言内存效率不高,但它确实节省了数据库命中和处理时间。

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:

$countries = file_get_contents('countries.html');    
echo str_replace('value="'.$userCountry.'"','value="'.$userCountry.'" SELECTED',$countries);

Not very memory efficient overall, but it does save the database hit and processing time.

喵星人汪星人 2024-08-04 15:26:00

您可以:

  • 使用 var_export() 将国家/地区数组打印为 PHP 代码,然后将其硬编码在某处。
  • 使用诸如 Pear Cache_Lite 之类的东西缓存数据库中的值 - 这非常容易使用,而且它意味着如果您修改数据库中的值,您所要做的就是删除缓存文件以重新生成它。

通过上述两个选项,您将获得一个数组,您可以按照与现在正在执行的操作类似的方式循环遍历该数组,以生成 html。

You could:

  • print an array of countries as PHP code using var_export(), and then hardcode that somewhere.
  • Cache the values from the database using something like Pear Cache_Lite - this is very straightforward to use and it means that if you modify the values in the database, all you have to do is delete the cache file to have it regenerated.

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.

紧拥背影 2024-08-04 15:25:52

像这样的事情怎么样?

<?
$countries = array(
"217" => "Turkenistan",
"218" => "Turks and Caicos Islands",
"219" => "Tuvalu",
"220" => "Uganda",
"221" => "Ukraine",
"222" => "United Arab Emirates",
"223" => "United Kingdom (Great Britain)"
"224" => "United States");
?>

<select name="country" style="width:180px;" onChange="do_get_rest_popup(this.value)" /> 
<?php
$countryCounter = 1;
$amtOfCountries = count($countries);
foreach ($country as $id => $c) {
    if ($countryCounter == $amtOfCountries) { 
    echo "<option value=\"$id\" SELECTED>$c</option>";
    } 
    else {
    echo "<option value=\"$id\">$c</option>";
    $countryCounter++;
        }
}
?>
</select>

编辑:我没有测试这个,但你应该明白

How about something like this?

<?
$countries = array(
"217" => "Turkenistan",
"218" => "Turks and Caicos Islands",
"219" => "Tuvalu",
"220" => "Uganda",
"221" => "Ukraine",
"222" => "United Arab Emirates",
"223" => "United Kingdom (Great Britain)"
"224" => "United States");
?>

<select name="country" style="width:180px;" onChange="do_get_rest_popup(this.value)" /> 
<?php
$countryCounter = 1;
$amtOfCountries = count($countries);
foreach ($country as $id => $c) {
    if ($countryCounter == $amtOfCountries) { 
    echo "<option value=\"$id\" SELECTED>$c</option>";
    } 
    else {
    echo "<option value=\"$id\">$c</option>";
    $countryCounter++;
        }
}
?>
</select>

EDIT: I did not test this but you should get the idea

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文