PHP - 链表"动态"



从数据库中读取时,我对链表有疑问, 让我用一个例子来解释。

示例数据库:

Country | City
USA     | New York
USA     | Washington
USA     | Dallas
USA     | LA
SPAIN   | Madrid
SPAIN   | Barcelona
UK      | London
UK      | Birmingham
UK      | Liverpool

HTML:2 选择,对于此示例,第一个是国家/地区,并且 秒是该国的城市。我的怀疑是我是否可以检查 一切动态,例如:

PHP 函数 1

$statement = $conexion->prepare("Select distinct Country from database");     $statement->execute();  $result1 =

$statement->fetchAll((;功能 2(接收$country(

enter code here
$statement = $conexion->prepare("Select city from database where country = :country");    $statement->execute(array(":country" =>

$country((; $result 2 = $statement->fetchAll((;

1º 想法:由于数据库不会被编辑,第一个想法是得到 不同变量中的所有选项,例如,$country和 $cityUSA、$citySPAIN和$cityUK,并在需要时加载它。

2º 想法:我想在我时将所需的城市加载到选项中 单击第一个选项,PHP 变量取值和 只做了 1 条语句,但我不知道怎么做,因为只有 PHP 采用 GET 和 POST 选项。

对不起我的英语,谢谢大家!

真实数据是下一个: 表项目

c_project_id | d_name | d_description | n_budget | d_state
1      |  Test  |  Test Project |    100   |  Open
2      |  Web   |    Web APP    |   3000   |  Open
3      | C Test |Closed Project |    100   | Closed
4      | Certif.| Certificates  |   2500   |  Open

表类别(与表项目连接(

c_category_id | d_name | d_description | c_project_id
1        | General| General cat   |      1
2        | Test   | Test cat      |      1
3        | General| General cat   |      2
4        | General| General cat   |      3
5        | Nothing| Nothing cat   |      3
6        |Program | Programming   |      2
...

我想要一个采用项目名称的 html 中的 SELECT,我采用它: .PHP

$statement2 = $conexion->prepare("SELECT c_project_id, d_name FROM project WHERE d_state= 'Open'");
$statement2->execute();
$resultado2 = $statement2->fetchAll();

和 HTML 代码中的 PHP foreach,这有效,我放置了下一个数据。

<option value=<?php echo $res['c_project_id'] ?>><?php echo $res['d_name'] ?></option>

现在我想要:当我在第一个选择中"单击"时,第二个选择使语句像那样

$statement2 = $conexion->prepare("SELECT c_category_id, d_name FROM category WHERE c_project_id = :id");

但是我不知道如何在不提交带有 _GET 美元或 _POST 美元的表格的情况下做到这一点,然后用 dinamycal 的方式进行。 我希望现在我解释得更好。

首先,你应该规范化你的数据库。这意味着将表格一分为二,一张用于国家,一张用于城市。

为您的国家/地区提供一个唯一的主键列(并且可能是自动递增(。

CountryId | CountryTag | CountryName
1 | USA        | United States of America
2 | ESP        | Spain
3 | UK         | United Kingdom

然后创建一个包含国家/地区和城市的多对多表,其中CountryId是引用您的国家/地区表的外键。

CountryId | CityName
1 | New York
1 | Washington
1 | Dallas
1 | Los Angeles
2 | Madrid
2 | Barcelona
3 | London
3 | Birmingham
3 | Liverpool

现在,您可以使用两个查询构造域模型:

$countries = array();
$statement = $connection->prepare("SELECT * FROM countries");
$statement->execute();
while ($row = = $statement->fetch(PDO::FETCH_ASSOC)) {
$countries[$row['CountryId']] = $row;
$countries[$row['CountryId']]['cities'] = array();
}
$statement = $connection->prepare("SELECT * FROM cities");
$statement->execute();
while ($row = = $statement->fetch(PDO::FETCH_ASSOC)) {
$countries[$row['CountryId']]['cities'][] = $row;
}

如果您使用JOIN,也可以在一个查询中执行此操作。

您现在有这个数组:

array(3) {
[1]=>
array(4) {
["CountryId"]=>
int(1)
["CountryTag"]=>
string(3) "USA"
["CountryName"]=>
string(24) "United States of America"
["cities"]=>
array(4) {
[0]=>
string(8) "New York"
[1]=>
string(10) "Washington"
[2]=>
string(6) "Dallas"
[3]=>
string(11) "Los Angeles"
}
}
[2]=>
array(4) {
["CountryId"]=>
int(2)
["CountryTag"]=>
string(3) "ESP"
["CountryName"]=>
string(5) "Spain"
["cities"]=>
array(2) {
[0]=>
string(6) "Madrid"
[1]=>
string(9) "Barcelona"
}
}
[3]=>
array(4) {
["CountryId"]=>
int(3)
["CountryTag"]=>
string(2) "UK"
["CountryName"]=>
string(14) "United Kingdom"
["cities"]=>
array(2) {
[0]=>
string(6) "London"
[1]=>
string(9) "Liverpool"
}
}
}

您现在可以根据需要循环访问它,它按CountryId索引,并在其中包含国家/地区名称和国家/地区内的城市。

1º 想法:由于数据库不会被编辑,第一个想法是获取不同变量中的所有选项,例如$country和$cityUSA,$citySPAIN和$cityUK,并在需要时加载它。

那为什么要为数据库而烦恼呢? 你一个可以编辑以更改变量的配置文件。为什么$cityUSA$citySPAIN$cities['spain']$cities['USA']同样有效,但您不必在代码中对国家/地区进行硬编码。

2º 想法:当我单击第一个选项时,我想将所需的城市加载到选项中,PHP 变量获取值并仅做出 1 条语句,但我不知道如何,因为 PHP 只接受 GET 和 POST 选项。

通过上述操作,您可以在选择中创建选项组,也可以将其作为 JSON 输出到站点源中,以使用 JavaScript 动态更改选择。 或者您通过 AJAX 加载城市。

工作片段:

var countries = {"1":{"CountryId":1,"CountryTag":"USA","CountryName":"United States of America","cities":["New York","Washington","Dallas","Los Angeles"]},"2":{"CountryId":2,"CountryTag":"ESP","CountryName":"Spain","cities":["Madrid","Barcelona"]},"3":{"CountryId":3,"CountryTag":"UK","CountryName":"United Kingdom","cities":["London","Liverpool"]}};
var country_select = document.getElementById('countries');
var city_select = document.getElementById('cities');
// react to changes in the country select
country_select.onchange = function() {
var elem = (typeof this.selectedIndex === "undefined" ? window.event.srcElement : this);
var value = elem.value || elem.options[elem.selectedIndex].value;

// empty cities select
city_select.innerHTML = '';

// populate with the correct cities;
	var country = countries[value];
country['cities'].forEach(function(element) {
	var option = document.createElement("option");
	option.value = element;
	option.text = element;
	city_select.appendChild(option);
});  
}
<select id="countries">
<option value="1">USA</option>
<option value="2">Spain</option>
<option value="3">UK</option>
</select>
<select id="cities">
<option value="New York">Los Angeles</option>
<option value="Washington">Washington</option>
<option value="Dallas">Dallas</option>
<option value="Los Angeles">Los Angeles</option>
</select>

最新更新