我需要以下代码的帮助 - 我试图显示其中一个类别,例如' genre'或者'平台'当用户从第一个下拉菜单中选择其中一个时。我不知道我会怎么做
<?php # browser_catalog.php
include ('includes/session.php');
$page_title = 'Browser Games Catalog';
include ('includes/header.php');
require_once ('mysqli_connect.php'); // Connect to the db.
// Check if the form has been submitted:
if (isset($_POST['submitted'])){
$errors = array(); // Initialize an error array.
// Check for a game name:
if (empty($_POST['cat1'])) {
$errors[] = 'You forgot to enter game name.';
} else {
$where = mysqli_real_escape_string($dbc, trim($_POST['cat1']));
}
if (empty($_POST['cat2'])) {
$errors[] = 'You forgot to enter game name.';
} else {
$what = mysqli_real_escape_string($dbc, trim($_POST['cat2']));
}
if($where == "Genre"){
$q = "SELECT game_name AS game,
platform AS pf,
genre AS ge,
game_description AS gd,
game_price AS gp,
game_image AS gi,
game_headlines AS gh
FROM game
WHERE genre = '$what'";
}
else{
$q = "SELECT game_name AS game,
platform AS pf,
genre AS ge,
game_description AS gd,
game_price AS gp,
game_image AS gi,
game_headlines AS gh
FROM game
WHERE platform = '$what'";
}
$r = \mysqli_query($dbc, $q); // Run the query.
// Count the number of returned rows:
$num = mysqli_num_rows($r);
if ($num > 0) { // If it ran OK, display the records.
echo "<p>There are currently $num number of games for '$search'.</p>\n";
// Table header.
echo '<table align="center" cellspacing="3" cellpadding="3" width="75%">
<tr>
<br> 	<td align="left" padding-left:5px><b>Game Name</b></td>
<br> 	<td align="left" padding-left:5px><b>Platform</b></td>
<br>	<td align="left"><b>Genre</b></td>
<br> 	<td align="left"><b>Game Description</b></td>
<br> 	<td align="left"><b>Game Price</b></td>
<br> 	<td align="left"><b>Game Image</b></td>
<br> 	<td align="left"><b>Game Headlines</b></td>
</tr>';
// Fetch and print all the records:
//<td align="left">' . "<img src = images/web/" . $row['gi'] "/>". '</td>
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo '<tr>
<br> 	<td align="left">' . $row['game'] . '</td>
<br> 	<td align="left">' . $row['pf'] . '</td>
<br> 	<td align="left">' . $row['ge'] . '</td>
<br> 	<td align="left">' . $row['gd'] . '</td>
<br> 	<td align="left">' . $row['gp'] . '</td>
<br> <td align="left">' . $row['gh'] . '</td>
</tr>';
}
echo '</table>'; // Close the table.
mysqli_free_result ($r); // Free up the resources.
} else { // If no records were returned.
echo '<p class="error">There are currently no registered users.</p>';
}
mysqli_close($dbc); // Close the database connection.
}
?>
<div class="page-header">
<h1 style="font-size:40px"><center></center></h1>
</div>
<form class="form-signin" role="form" action="browser_catalog.php" method="post">
<?php
<p>Choose a Category:
<select class="form-control" id="element_6" name="category" value="<?php if (isset($_POST['cat1'])) echo $_POST['cat1']; ?>" />
<option>Platform</option>
<option>Genre</option>
</select>
</p>
if($cat1 == "Platform"){
<p>Platform:
<select class="form-control" id="element_6" name="platform" value="<?php if (isset($_POST['cat2'])) echo $_POST['cat2']; ?>" />
<option >PC</option>
<option >PS4</option>
<option >Xbox One</option>
<option >Wii U</option>
<option >PS3</option>
<option >Xbox 360</option>
<option >Wii</option>
<option >Mac</option>
<option >Linux</option>
<option >Nintendo 3DS</option>
<option >DS</option>
<option >PS2</option>
<option >iPhone</option>
<option >Android</option>
</select>
</p>
}
else{
<p>Genre:
<select class="form-control" id="element_6" name="genre" value="<?php if (isset($_POST['cat2'])) echo $_POST['cat2']; ?>">
<option>Action</option>
<option>Adventure</option>
<option>Driving</option>
<option>RPG</option>
<option>Strategy</option>
<option>Sport</option>
<option>Puzzle</option>
</select>
</p>
}
?>
<button style="display:inline; padding:10px; margin-bottom:5px" type="submit" name="submit" class="btn btn-sm btn-primary" />Search Game</button>
<input type="hidden" name="submitted" value="TRUE" /></p></center>
</form>
<?php
include ('includes/footer.html');
?>
答案 0 :(得分:0)
首先你有一个typpo
<p>Choose a Category:
<select class="form-control" id="element_6" name="category" value="<?php if (isset($_POST['cat1'])) echo $_POST['cat1']; ?>" />
<option value=>Platform</option>
<option >Genre</option>
</select>
</p>
这里:
<option value=>Platform</option>
然后你在写php的时候没有打开php标签。这里的例子
if($cat1 == "Platform"){
在此之前,在html结束之后你应该像这样打开php标签
<?php
if($cat1 == "Platform"){
同样在很多地方你混合html和php
此处混合html和php。没有打开和关闭php标签
</p>
}
else{
<p>Genre:
另外,我建议您使用<input type='submit'>
代替<button type='submit'>
来提交表单。
无论如何,编辑你的问题并修复你的代码语法会很好,然后我们可以专注于你的问题,你需要更精确地解决代码真正的问题。 谢谢 编辑: 这是一个如何做的例子
// not here remove this line -> <?php
<p>Choose a Category:
<select class="form-control" id="element_6" name="category" value="<?php if (isset($_POST['cat1'])) echo $_POST['cat1']; ?>" />
<option>Platform</option>
<option>Genre</option>
</select>
</p>
// But instead here put <?php
if($cat1 == "Platform"){
// And here close it -> ?>
休息你必须为自己做,因为我不能为你编写完整的代码,也有助于你学习你自己的错误,如果可以的话,我可以在这里提供帮助。 阅读w3学校网站,在那里你可以找到如何正确编写php和html的基础知识