Я использую 3 таблицы (Region
, Country
, Market Strategy
).У меня есть функции php marketStrategyT
и strategyExists
.
Функция php marketStrategyT
получает все операторы для выпадающего меню.
Функция php strategyExists
проверяет, есть ли у нас рыночная стратегия либо в выбранном регионе, либо в выбранномстрана.
Вот проблема
В раскрывающемся меню не отображается список рыночной стратегии для региона и для каждой страны.Ничего не отображаетсяС некоторым эхом я вижу, что первый регион и первая страна заняты функцией marketStrategyT, показывают правильную информацию с помощью стратегии StrategyExists.Ничего не отображается, потому что первая страна (Америка - Аргентина) не имеет рыночной стратегии.Тем не менее, пока не смотрит на оставшуюся страну / регион.
Вот что должна делать функция marketStrategyT
(1) Получение всего региона из базы данных Region
.
(2) Использование функции strategyExists
, чтобы увидеть, есть ли у нас рыночная стратегия в этом конкретном регионе.
- функция StrategyExists возвращает
FALSE
=> у нас нет рыночной стратегии в этом регионе (переход к пункту 1) - функция StrategyExists возвращает
TRUE
=> мы имеемрыночная стратегия в этом регионе (перейти к пункту 3)
(3) Получение всей страны из базы данных Country
.
(4) Использование функции strategy Exists
для того, чтобыпосмотрим, есть ли у нас рыночная стратегия в этой конкретной стране
- функция StrategyExists возвращает
FALSE
=> у нас нет рыночной стратегии в этой стране (перейдите к пункту 3). - функция 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;
}
Функция 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;
}