-3

最初はうまく説明できませんでした。もう一度やってみます。

これらの if ステートメントを for ループで単純化したいのです。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