dongshi1869 2014-08-13 15:16
浏览 35
已采纳

从文本文件填充下拉列表,值被设置为递增数而不是内容

I've managed to pull together a way to populate a 'Country' dropdown menu from a txt file, with all of the options on a line each.

The value of each item is given an incrementing number, and passes this through fine.

But how can I pass through the contents of the line from the text file, without having to create a massive switch for each of the number values, to set the country?

<select class="country-select" name="country" tabindex = '9' >
<?php
$lines = file( 'country-list.txt' );
    for ($i = 0; $i < count($lines);$i++) {
        echo '<option value=' . ($i + 1) . '>' . $lines[$i] . '</option>';
    }
?>
</select>

$name       = clearData($_POST["name"]);
$country    = ($_POST["country"]);
$phone      = ($_POST["phone"]);

A small portion of my country file:

Your Country
-
Afghanistan
Albania
Algeria
American Samoa
Andorra
Angola
Anguilla
Antarctica
Antigua/Barbuda
Argentina
Armenia
Aruba
Australia

It's worth saying, that the email response I receive, shows the number of the value. Which is the line number in the text file, and that corresponds to the correct country chosen in the dropdown menu. So it is selecting and passing through the correct country.

  • 写回答

1条回答 默认 最新

  • dongpin6941 2014-08-13 15:47
    关注

    Try using these functions; it should work for your purposes.

    // Get an array containing valid countries.
    function GetCountries()
    {
        $lines = file('country-list.txt');
        return $lines;
    }
    
    // Get the name of the country from the specified line number (its index in the array)
    function GetCountryName($countryIndex)
    {
        $countries = GetCountries();
        // It looks like your values for the <select> elements are not zero-based, so you might want to apply that modification here.  Uncomment the following line if that is the case.
        // $countryIndex = $countryIndex - 1;
        $countryName = $countries[$countryIndex];
        return $countryName;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码