Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中,如何在for循环中连接两个字符串?_Php - Fatal编程技术网

在PHP中,如何在for循环中连接两个字符串?

在PHP中,如何在for循环中连接两个字符串?,php,Php,我第一次解释得不是很好。我再试一次 我想用for循环简化这些if语句。CatagoryX是(1-3),所以我想我可以循环遍历变量,但我不知道如何编写代码 我想用for循环来简化这个过程 $sqlCommand = "SELECT id, catagory1, catagory2, catagory3 FROM listofbooks; $query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); w

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

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

}
你不需要一个循环。 试试这个

$bookCategories = Array("18"=>"Thriller", "19"=>"Fantasy", "20"=>"Science Fiction");
while($row = mysqli_fetch_array($query)){
    $bookcategory[] = $bookCategories[$category];
}

提示:
foreach($cat行){/*code!*/}
这与MySQL无关,与没有很好的PHP引用有关。这看起来像是代码,但没有任何意义。当CSI想要拍摄“黑客”镜头时,屏幕上就会出现这种东西。嘿嘿。。。也谢谢你的拼写帮助。:)优雅的回答。不过,这产生了一个奇怪的效果。在下面的echo中,$bookcatagoryX生成一个
。下面是代码:
/p>
输出如下所示(当数字为18、19、20时)。即使我使用惊悚片(马车返回)幻想(马车返回)科幻小说(马车返回),弦内是否有一个
创造<代码>