![](/img/trans.png)
[英]How do I display Text in HTML based on an option selected from a Drop-Down Menu?
[英]How do I display one of the categories from selected drop-down
我需要以下代碼的幫助-當用戶從第一個下拉菜單中選擇“流派”或“平台”等類別時,我試圖顯示其中一個類別。 我不知道該怎么做
<?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');
?>
首先你有錯字
<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的基礎知識
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.