首页 > 解决方案 > php - 包含“if”条件的 while 语句被数据库中的第一项阻塞并且不递增

问题描述

我正在使用 3 个表(Region, Country, Market Strategy)。我有函数 phpmarketStrategyTstrategyExists.

函数 phpmarketStrategyT获取下拉菜单的所有语句。
函数 phpstrategyExists检查我们是否在所选地区或所选国家/地区有市场策略。

这是问题

下拉菜单不显示每个地区和每个国家的市场策略列表。什么都没有显示。有了一些回声,我可以看到第一个地区和第一个国家是由函数 marketStrategyT 获取的,用函数 strategyExists 显示正确的信息。没有显示任何内容,因为第一个国家(美国 - 阿根廷)没有任何市场策略。但是,while 不考虑剩余的国家/地区。

这是函数marketStrategyT应该做什么

(1) 从数据库中获取所有Region Region
(2) 使用该功能strategyExists来查看我们在该特定区域是否有市场策略。

(3) 从数据库中获取所有国家Country
(4) 使用该功能strategy Exists来查看我们在这个特定国家是否有市场策略

(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;
}

标签: phpif-statementwhile-loop

解决方案


下面的逻辑strategyExists()可能并不理想。

if ($type == 'region') {
    $strategy=$bdd->query("SELECT * FROM market_strategy WHERE region_id='".$val."' ORDER BY name");
    while($data=$strategy->fetch()) {
        $return=True;
    }
  1. $strategy_fetch() 使用默认参数 PDO::FETCH_BOTH,即它与返回数组或 False 的 $strategy_fetch(PDO::FETCH_BOTH) 相同。因此尚不清楚对返回值进行逻辑测试的结果是什么。

  2. 您不需要加载完整的结果集来查看是否有任何行。最好使用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;
}

如果代码中有任何拼写错误,请提前道歉,因为我无法测试。


推荐阅读