我第一次没有很好地解释。我会再试一次。
我想用for循环简化这些if语句。 CatagoryX是(1-3)所以我认为我可以遍历变量,但我不知道如何编写代码。
我想用for循环来简化它。
$sqlCommand = "SELECT id, catagory1, catagory2, catagory3 FROM listofbooks;
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
while($row = mysqli_fetch_array($query)){
//check to see if catagory1 is an 18, 19, or 20, and make $bookcatagory=the name
if (catagory1 == "18"){
$bookcatagory == "Thriller";
catagory1 = "";
}
if (catagory1 == "19"){
$bookcatagory == "Fantasy";
catagory1 = "";
}
if (catagory1 == "20"){
$bookcatagory == "Science Fiction";
catagory1 = "";
}
//check to see if catagory2 is an 18, 19, or 20, and make $bookcatagory=the name
if (catagory2 == "18"){
$bookcatagory2 == "Thriller";
catagory2 = "";
}
if (catagory2 == "19"){
$bookcatagory2 == "Fantasy";
catagory2 = "";
}
if (catagory2 == "20"){
$bookcatagory2 == "Science Fiction";
catagory2 = "";
}
//check to see if catagory2 is an 18, 19, or 20, and make $bookcatagory=the name
if (catagory3 == "18"){
$bookcatagory2 == "Thriller";
catagory3 = "";
}
if (catagory3 == "19"){
$bookcatagory2 == "Fantasy";
catagory3 = "";
}
if (catagory3 == "20"){
$bookcatagory2 == "Science Fiction";
catagory3 = "";
}
}
答案 0 :(得分:0)
您不需要循环。 试试这个!
$bookCategories = Array("18"=>"Thriller", "19"=>"Fantasy", "20"=>"Science Fiction");
while($row = mysqli_fetch_array($query)){
$bookcategory[] = $bookCategories[$category];
}