我想在某些情况下分配一些值,请帮助我,我现有的代码是
while($local_data = mysqli_fetch_assoc($query) ){
$category = explode('->',$local_data['breadcrumbs']);
echo $category[2]; echo "<br />";
if(strtolower(trim($category[2])) =='outdoor'){
$cat_ID = 5;
}
if(strtolower(trim($category[2]))=='furniture'){
$cat_ID = 20;
}
if(strtolower(trim($category[2]))=='Bathroom'){
$cat_ID = 6;
}
if(strtolower(trim($category[2]))=='Furniture'){
$cat_ID = 15;
}
if(strtolower(trim($category[2]))=='Office'){
$cat_ID = 62;
}
if(strtolower(trim($category[2]))=='Contract'){
$cat_ID = 81;
}
if(strtolower(trim($category[2]))=='Decor'){
$cat_ID = 90;
}
if(strtolower(trim($category[2]))=='Construction'){
$cat_ID = 108;
}
if(strtolower(trim($category[2]))=='Tech'){
$cat_ID = 133;
}
echo $cat_ID; echo "<br />";
} 现在它不会进入if条件
我已经编辑了代码,
答案 0 :(得分:1)
您最好在数组中创建类别的“地图”,而不是编写大量if's
。这样,如果要测试是否找到类别,您只需要一个
<?php
$categories = [
'outdoor' => 5,
'furniture' => 20,
'bathroom' => 6,
'furniture' => 15,
'office' => 62,
'contract' => 81,
'decor' => 90,
'construction' => 108,
'tech' => 133,
];
while($local_data = mysqli_fetch_assoc($query)){
$category = explode('->',$local_data['breadcrumbs']);
$category_id = isset($categories[trim(strtolower($category[2]))]) ? $categories[trim(strtolower($category[2]))] : null;
echo 'Category ID = '. ($category_id ?: 'Not found');
}
答案 1 :(得分:0)
您需要放置以下条件
if(strtolower(trim($category[2])) =='outdoor'){
$cat_ID = 5;
exit();
}