-3

我第一次解释得不是很好。我会再尝试。

我想用 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 = "";
}

}
4

1 回答 1

0

你不需要循环。尝试这个!

$bookCategories = Array("18"=>"Thriller", "19"=>"Fantasy", "20"=>"Science Fiction");
while($row = mysqli_fetch_array($query)){
    $bookcategory[] = $bookCategories[$category];
}
于 2013-09-25T19:32:00.563 回答