【发布时间】:2016-03-02 15:52:30
【问题描述】:
我已经查看了很多问题,如果有任何问题可以提供帮助,请原谅我,但我无法弄清楚这一点。
所以我有一个简单的 HTML 表单,它接受用户输入的三个类别:多人游戏、平台和流派。这是html代码
<form method="post" action="gamebuddy.php">
<div class="players">
Please select one (or both) of the following: <br>
<input type="checkbox" name="players[]" value="No">Single Player<br>
<input type="checkbox" name="players[]" value="Yes">Multiplayers<br>
</div>
<div class="platform">
Please select your game system(s): <br>
<input type="checkbox" name="platform[]" value="XBOX">Xbox<br>
<input type="checkbox" name="platform[]" value="PS3">PS3<br>
<input type="checkbox" name="platform[]" value="PC">PC<br>
<input type="checkbox" name="platform[]" value="Wii">Wii<br>
</div>
<div class="genre">
Please select the genre(s) of game you would like: <br>
<input type="checkbox" name="genre[]" value="Action" >Action<br>
<input type="checkbox" name="genre[]" value="Casual">Casual<br>
<input type="checkbox" name="genre[]" value="Roleplaying">Role-Playing<br>
<input type="checkbox" name="genre[]" value="Shooter">Shooter<br>
<input type="checkbox" name="genre[]" value="Sports">Sports<br>
</div>
<div class="submit">
<input type="submit">
</div>
然后我有一个 PHP 文件,当用户单击提交时使用该文件。理想情况下,它将表单输入作为变量,并使用 SQLite 语句根据用户的选择查找用户可以玩的游戏。
这是 PHP 代码:
<div class="displaygames">
Based on your choices, these games seem suitable for you: <br>
<?php
if(!empty($_POST['players'])) {
foreach($_POST['players'] as $players) {
echo $players; //echoes the value set in the HTML form for each checked checkbox.
//so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
//in your case, it would echo whatever $row['Report ID'] is equivalent to.
}
}
if(!empty($_POST['platform'])) {
foreach($_POST['platform'] as $platform) {
echo $platform; //echoes the value set in the HTML form for each checked checkbox.
//so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
//in your case, it would echo whatever $row['Report ID'] is equivalent to.
}
}
if(!empty($_POST['genre'])) {
foreach($_POST['genre'] as $genre) {
echo $genre; //echoes the value set in the HTML form for each checked checkbox.
}
}
//This is to connect to the database
$db = new SQLite3('gamebuddy.db');
//Statement that uses variables to create list
$results = $db->query("SELECT * FROM games where multiplayer = '$players' and platform = '$platform' and genre is '$genre'");
//Displays List
while ($row = $results->fetchArray()) {
echo '<ul>';
echo $row['game'];
echo '</ul>';
}
?>
因此,如果您为每个类别输入一个答案,一切都会正常进行(例如,用户单击“否”进入多人游戏,“PS3”进入平台,“动作”进入类型)。但是如果用户选择“动作”和“角色扮演”,由于某种原因,它只需要用户选择的最后一个,在这种情况下是“角色扮演”。
所以我的问题是,当有多个输入时,如何获得显示所有游戏的语句。
感谢您的帮助,我会回答可能存在的任何问题,如果有帮助,当然将答案标记为已解决。谢谢!
【问题讨论】: