mercredi 3 octobre 2018

php - the while statement including a "if" condition is block with the first item from the database and doesn't increment

I am using 3 tables (Region, Country, Market Strategy). I have the function php marketStrategyT and strategyExists.

The function php marketStrategyT get all the statements for the drop-down menu.
The function php strategyExists check if we do have a market strategy either in the selected region, or in the selected country.

Here is the problem

The drop-down menu doesn't display the list of market strategy per region and per country. Nothing is displayed. With some echo, I can see the first region and the first country are taken by the function marketStrategyT, show the correct information with the function strategyExists. Nothing is displayed because the first country (America - Argentina) doens't have any market strategy. However, the while doesn't look at the remaining country/region.

Here is what the function marketStrategyT should do

(1) Getting all the Region from the database Region.
(2) Using the function strategyExists in order to see if we have a market strategy in this specific region.

  • function strategyExists return FALSE => we don't have a market strategy in this region (go to point 1)
  • function strategyExists return TRUE => we have a market strategy in this region (go to point 3)

(3) Getting all the Country from the database Country.
(4) Using the function strategy Exists in order to see if we have a market strategy in this specific country

  • function strategyExists return FALSE => we don't have a market strategy in this country (go to point 3).
  • function strategyExists return TRUE => we have a market strategy in this country (go to point 5).

(5) Display the name of the market strategy for the drop down list.

Here the code 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;
}

function 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)
    // 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;
}

Aucun commentaire:

Enregistrer un commentaire