首先,我创建了5个标准来评价一个人。每个标准可以用3种方式评定,即低,中,高。现在,我想根据这5个标准创建一个组合。这创造了243组合(从3 ^ 5)
例如:
第一个组合:
标准1:低,
标准2:低,
标准3:低,
标准4:低,
标准5:低
第二个组合:
标准1:低,
标准2:低,
标准3:低,
标准4:低,
标准5:高
...
第243组合
标准1:高,
标准2:高,
标准3:高,
标准4:高,
标准5:高
我创建它的代码工作正常,但是当我尝试使其动态化以便可以通过使用递归来修改条件的数量(条件来自数据库),它不能很好地工作。这是我的静态代码(仅适用于5个标准):
$criteria1 = array('low'=>0,'medium'=>1,'high'=>2);
$criteria2 = array('low'=>0,'medium'=>1,'high'=>2);
$criteria3 = array('low'=>0,'medium'=>1,'high'=>2);
$criteria4 = array('low'=>0,'medium'=>1,'high'=>2);
$criteria5 = array('low'=>0,'medium'=>1,'high'=>2);
$level = array('low'=>0,'medium'=>1,'high'=>2);//buat give integer value to low, high, medium
foreach($criteria1 as $key1=>$a){
foreach($criteria2 as $key2=>$b){
foreach($criteria3 as $key3=>$c){
foreach($criteria4 as $key4=>$d){
foreach($criteria5 as $key5=>$e){
$warna[] = array('criteria1'=>$key1,'criteria2'=>$key2,'criteria3'=>$key3,'criteria4'=>$key4,'criteria5'=>$key5);//to get 5 criteria combination per row
if( $level[$key1] >= $level['medium'] && $level[$key2] == $level['medium'] && $level[$key3] >= $level['medium'] && $level[$key4] >= $level['medium'] && $level[$key5] == $level['medium']){
$rule[] = "DPM1";
} else if(( $level[$key1] >= $level['medium'] && $level[$key2] > $level['medium'] && $level[$key3] >= $level['medium'] && $level[$key4] >= $level['medium'] && $level[$key5] == $level['medium'])||
($level[$key1] >= $level['medium'] && $level[$key2] == $level['medium'] && $level[$key3] >= $level['medium'] && $level[$key4] >= $level['medium'] && $level[$key5] > $level['medium']))
{
$rule[] = "DPM2";
}
else if($level[$key1] >= $level['medium'] && $level[$key2] > $level['medium'] && $level[$key3] >= $level['medium'] && $level[$key4] >= $level['medium'] && $level[$key5] > $level['medium']){
$rule[] = "DTM";
}
else{
$rule[] = "TD";
}
}
}
}
}
}
这是我的动态代码,但它仍然不起作用
//in main
$res = mysql_query("SELECT * FROM kriteria") or die(mysql_error());
$totalcriteria = mysql_num_rows($res);
while($row = mysql_fetch_array($res)){
$full[] = $row['nama'];//get the criteria name from database and fill in full
}
createCombination($totalcriteria,$full);
/* ...... */
function createCombination($index,$full){
if($index > 0){
$index = $index-1;
$thiskriteria = $full[$index];
$thiskriteria = array("rendah"=>0,"sedang"=>1,"tinggi"=>2);
foreach($thiskriteria as $key=>$a){
createCombination($index,$full);
}
} else{//create the combination
$warna[] = array('criteria1'=>$key1,'criteria2'=>$key2,'criteria3'=>$key3,'criteria4'=>$key4,'criteria5'=>$key5);
if( $tingkat[$key1] >= $tingkat['sedang'] && $tingkat[$key2] == $tingkat['sedang'] && $tingkat[$key3] >= $tingkat['sedang'] && $tingkat[$key4] >= $tingkat['sedang'] && $tingkat[$key5] == $tingkat['sedang']){
$rule[] = "DPM1";//rule buat nampung list terima ato kaga
} else if(( $tingkat[$key1] >= $tingkat['sedang'] && $tingkat[$key2] > $tingkat['sedang'] && $tingkat[$key3] >= $tingkat['sedang'] && $tingkat[$key4] >= $tingkat['sedang'] && $tingkat[$key5] == $tingkat['sedang'])||
($tingkat[$key1] >= $tingkat['sedang'] && $tingkat[$key2] == $tingkat['sedang'] && $tingkat[$key3] >= $tingkat['sedang'] && $tingkat[$key4] >= $tingkat['sedang'] && $tingkat[$key5] > $tingkat['sedang']))
{
$rule[] = "DPM2";
}
else if($tingkat[$key1] >= $tingkat['sedang'] && $tingkat[$key2] > $tingkat['sedang'] && $tingkat[$key3] >= $tingkat['sedang'] && $tingkat[$key4] >= $tingkat['sedang'] && $tingkat[$key5] > $tingkat['sedang']){
$rule[] = "DTM";
}
else{
$rule[] = "TD";
}
}
}