【问题标题】:How to create drop down menu [closed]如何创建下拉菜单[关闭]
【发布时间】:2014-01-05 12:57:25
【问题描述】:

我正在尝试创建下拉菜单,这个下拉菜单是关于国家名称的,所以当用户例如选择国家 A 时,将显示与国家 A 相关联的所有帖子。

所以我的问题是我是否需要为每个国家/地区创建一个单独的 PHP 文件才能获取与特定国家/地区关联的帖子?

【问题讨论】:

  • 不,您只需将国家/地区名称或 id 传递给单个脚本,获取数据并显示它。
  • 你在做自动完成字段吗?

标签: php html mysql


【解决方案1】:

不,您会像往常一样使用

创建下拉列表
<select name="myCountry">
     <option>Country A</option>
     <option>Country B</option>
     <option>etc lol</option>
</select>

然后让您的表单指向您的 PHP 处理文件,然后您可以在其中执行类似操作。

$selectedCounty = $_GET['myCountry']; //This assigns the selected value from that country dropdown into a usable variable.

然后你查询数据库。 假设您有一个名为“countries”的数据库表和一个列有名为“myCountries”的国家/地区的列。

$selectCountryQS = SELECT * FROM countries WHERE myCountries = '$selectedCountry';

然后付诸行动

$selectCountryDoIt = mysqli_query('connection variable here', $selectCountryQS ) or die('error mssg'. mysqli_error(conection var here));

然后设置一个 while 循环,它将获取您想要的所有帖子数据。

while($row = mysqli_fetch_array($selectCountryDoIt)){
  echo $row[' your column data to display here'];
}

这将为您提供所需的东西。 希望这可以帮助。祝你好运

【讨论】:

  • 感谢您的解释:)
  • 当然很高兴它对你有用。欢迎来到 SO =)
猜你喜欢
  • 2018-03-04
  • 2011-12-09
  • 2015-02-16
  • 2017-01-25
  • 1970-01-01
  • 1970-01-01
  • 2021-06-24
  • 2019-12-05
  • 2020-01-24
相关资源
最近更新 更多