php - php - 包含“if”条件的 while 语句被数据库中的第一项阻塞并且不递增
问题描述
我正在使用 3 个表(Region
, Country
, Market Strategy
)。我有函数 phpmarketStrategyT
和strategyExists
.
函数 phpmarketStrategyT
获取下拉菜单的所有语句。
函数 phpstrategyExists
检查我们是否在所选地区或所选国家/地区有市场策略。
这是问题
下拉菜单不显示每个地区和每个国家的市场策略列表。什么都没有显示。有了一些回声,我可以看到第一个地区和第一个国家是由函数 marketStrategyT 获取的,用函数 strategyExists 显示正确的信息。没有显示任何内容,因为第一个国家(美国 - 阿根廷)没有任何市场策略。但是,while 不考虑剩余的国家/地区。
这是函数marketStrategyT
应该做什么
(1) 从数据库中获取所有Region Region
。
(2) 使用该功能strategyExists
来查看我们在该特定区域是否有市场策略。
- function strategyExists return
FALSE
=> 我们在该地区没有市场策略(转到第 1 点) - function strategyExists return
TRUE
=> 我们在该区域有一个市场策略(转到第 3 点)
(3) 从数据库中获取所有国家Country
。
(4) 使用该功能strategy Exists
来查看我们在这个特定国家是否有市场策略
- function strategyExists return
FALSE
=> 我们在这个国家没有市场策略(转到第 3 点)。 - function strategyExists return
TRUE
=> 我们在这个国家有一个市场策略(转到第 5 点)。
(5) 显示下拉列表的市场策略名称。
这里的代码php
// LIST OF MARKET STRATEGY AVEC COUNTRY
function marketStrategyT(){
$bdd=new PDO('mysql:host=localhost; dbname=workplan; charset=utf8', 'root','');
$marketStrategy_return=array();
// To select all regions
$region=$bdd->query("SELECT * FROM region ORDER BY region");
// ==> (1) <==
while($data_region=$region->fetch()){
// Definition of variables
$region_id=$data_region['region_id'];
$region=$data_region['region'];
// checking if there is any strategy for this region
// ==> (2) <==
$regionStrategyExists=strategyExists($region_id, 'region'); // should return 'true' or 'false'
if ($regionStrategyExists) {
// To display the name of the region in the drop-down menu
$marketStrategy_return[]="<option value=\"N/A\">" . $region . "</option>\n";
// To select all countries
$country=$bdd->query("SELECT * FROM country WHERE country_region_id='". $region_id ."' ORDER BY country");
// ==> (3) <==
while($data_country=$country->fetch()){
// Definition of variables
$country_id=$data_country['country_id'];
$country=$data_country['country'];
// checking if there is any strategy for this region
// ==> (4) <==
$countryStrategyExists=strategyExists($country_id, 'country');// should return 'true' or 'false'
if ($countryStrategyExists) {
// To display the name of the country in the drop-down menu
$marketStrategy_return[]="<option value=\"N/A\">" . $country . "</option>\n";
// To select all strategy
$strategy=$bdd->query("SELECT * FROM market_strategy WHERE region_id='" . $region_id."' AND country_id='".$country_id."' ORDER BY name");
// ==> (5) <==
while($data_strategy=$strategy->fetch()){
// Definition of variables
$market_strategy_id=$data_strategy['market_strategy_id'];
$market_strategy=$data_strategy['name'];
// inserting the name of the strategy
$marketStrategy_return[]="<option value=\"" . $market_strategy_id . "\">" . $market_strategy . "</option>\n";
}
}
}
}
}
return $marketStrategy_return;
}
功能策略存在
// STRATEGY EXISTS
function strategyExists($val, $type){
$bdd=new PDO('mysql:host=localhost; dbname=workplan; charset=utf8', 'root','');
// $val represent the id
// $type represent the table (region / country)
// Default value is False -> there is no strategy for this region / this country
$return=False;
// Checking if there is any strategy for the region
if ($type == 'region') {
$strategy=$bdd->query("SELECT * FROM market_strategy WHERE region_id='".$val."' ORDER BY name");
while($data=$strategy->fetch()) {
$return=True;
}
} elseif($type == 'country') { // Checking if there is any strategy for the country
$strategy=$bdd->query("SELECT * FROM market_strategy WHERE country_id='".$val."' ORDER BY name");
while($data=$strategy->fetch()) {
$return=True;
}
}
return $return;
}
解决方案
下面的逻辑strategyExists()
可能并不理想。
if ($type == 'region') {
$strategy=$bdd->query("SELECT * FROM market_strategy WHERE region_id='".$val."' ORDER BY name");
while($data=$strategy->fetch()) {
$return=True;
}
$strategy_fetch() 使用默认参数 PDO::FETCH_BOTH,即它与返回数组或 False 的 $strategy_fetch(PDO::FETCH_BOTH) 相同。因此尚不清楚对返回值进行逻辑测试的结果是什么。
您不需要加载完整的结果集来查看是否有任何行。最好使用
count(*)
which 返回行数,或者只是TOP 1
找到第一行。
strategyExists()
我不知道这是否会解决所有问题,但是您应该首先按照以下说明清理代码。
// STRATEGY EXISTS
function strategyExists($val, $type){
$bdd=new PDO('mysql:host=localhost; dbname=workplan; charset=utf8', 'root','');
// $val represent the id
// $type represent the table (region / country)
$return=True; // set to true and later change to False is tests fail
// Checking if there is any strategy for the region
if ($type == 'region') {
$strategy=$bdd->query("SELECT TOP 1 FROM market_strategy WHERE region_id='".$val."' ORDER BY name");
if (!$data=$strategy->fetch()) {
$return=False; // there are no results
}
} elseif($type == 'country') { // Checking if there is any strategy for the country
$strategy=$bdd->query("SELECT TOP 1 FROM market_strategy WHERE country_id='".$val."' ORDER BY name");
if (!$data=$strategy->fetch()) {
$return=False; // there are no results
}
}
return $return;
}
如果代码中有任何拼写错误,请提前道歉,因为我无法测试。
推荐阅读
- haskell - 是否可以将数据文件编译成最终的二进制文件?
- java - JPQL 原生查询结果无法映射到列表?
- python - TypeError:强制转换为 Unicode:需要字符串或缓冲区,找到 PosixPath
- php - 我在 MPDF 库中遇到 UTF-8 字符集问题
- python - 如何获取用户的电话号码?电报机器人
- javascript - 当 .size 不存在时,在 Node.js 中尝试从 WeakSet/WeakMap(通过垃圾收集)自动删除的项目?
- python - 计算二值图像中任何标记区域中两个像素之间的最大距离
- architecture - 微服务架构风格可以用作 SAD 的原则吗?
- java - 如何在点击 ViewPager 时打开详细片段?
- python - 没有语句的 Python for 循环语法