[英]How do I display one of the categories from selected drop-down
I need help with the code below - I'm trying to display one of the categories like 'genre' or 'platform' when the user selects one of them from the first drop-down menu. 我需要以下代码的帮助-当用户从第一个下拉菜单中选择“流派”或“平台”等类别时,我试图显示其中一个类别。 I don't know how I would do this
我不知道该怎么做
<?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');
?>
First you have a typpo here 首先你有错字
<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>
here: 这里:
<option value=>Platform</option>
Then you did not open php tags when writting php. 然后您在编写php时没有打开php标签。 example here
这里的例子
if($cat1 == "Platform"){
Before this, and after end of html you should open php tags like this 在此之前,以及在html结束之后,您应该打开像这样的php标签
<?php
if($cat1 == "Platform"){
Also on lots of places you mixing html and php 同样在很多地方,您混合使用html和php
Also here mixing html and php. 同样在这里混合html和php。 No opening and closing php tag
没有打开和关闭php标签
</p>
}
else{
<p>Genre:
Also I will recommend you to use <input type='submit'>
instead of <button type='submit'>
for submititng form. 我也建议您使用
<input type='submit'>
而不是<button type='submit'>
表单。
Anyway it will be good to edit your question and fix syntax on your code and then we can focus on your problem and you will need to be more precise what is really a problem with code. 无论如何,最好编辑您的问题并在代码上修复语法,然后我们可以专注于您的问题,您将需要更加精确地了解代码真正存在的问题。 Thanks Edit: Here is one example how to do it
谢谢编辑:这是一个例子
// 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 -> ?>
Rest you must do for yourself since I can not write full code for you, and is also good for you to learn on your own mistakes, and I can be here to provide help if I can. 休息吧,您必须为自己做些事情,因为我无法为您编写完整的代码,并且也有助于您学习自己的错误,如果可以,我可以在这里提供帮助。 Read w3 school website, there you can find basics of how to properly write php and html
阅读w3学校网站,在那里您可以找到如何正确编写php和html的基础知识
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.