【发布时间】:2017-02-24 14:33:45
【问题描述】:
我觉得我非常接近解决这个问题,但无法缩小差距。我有一个从 SQL 数据库中提取的列表,我希望能够从列表中选择一项,并且只显示表连接到的信息。一切都完全正确地拉起,除了列表中的所有项目都在拉动,而不仅仅是被选中的那一项。
<form action="#" method="post">
<table class="table">
<thead>Martial Weapon Name</thead>
<tr>
<th>
<select name="Choosen">
<?php
echo'<option>Select Weapon</option>';
//Check if at least one row is found
if($result->num_rows >0){
//Loop through results
while($row = $result->fetch_assoc()){
//Display weapon info
$output = $row['weapon_name'];
echo '<option>'.$output.'</option>';
}
}
?>
</select>
</th>
</tr>
</table>
<input class="btn btn-default" type="submit" name="submit" value="Generate">
<h3>Weapon</h3>
<table class="table table-striped">
<tr>
<th>Weapon Name</th>
<th>Weapon Type</th>
<th>Damage</th>
</tr>
<?php
$choose= "SELECT
weapon_types_martial.id,
weapon_types_martial.weapon_name,
weapon_types_martial.weapon_type,
weapon_types_martial.weapon_damage
FROM weapon_types_martial";
$result = $mysqli->query($choose) or die($mysqli->error.__LINE__);
if(isset($_POST['submit'])){
$selected_weapon = $_POST['Choosen'];
while($list = $result->fetch_assoc()){
//Display weapon
$show ='<tr>';
$show .='<td>'.$list['weapon_name'].'</td>';
$show .='<td>'.$list['weapon_type'].'</td>';
$show .='<td>'.$list['weapon_damage'].'</td>';
$show .='</tr>';
//Echo output
echo $show;
}
}
?>
</form>
以上是表格和表格的代码。同样,一切本身都在工作。数据库正在连接并拉起一切很棒的东西。我唯一要做的就是确保输出的数据只是从下拉列表中选择的项目。
以下是列表的屏幕截图。我目前(故意)在数据库中只有 3 个项目。所以我选择了战斧而不是战斧线,所有 3 个都出现了。
编辑 1
这里要求的是完整的页面代码。整个页面都可以正常工作,数据库正在连接等等。只是输出没有放 1 选择的武器。遵循一些先前建议的更改,根本停止将武器输入到表格中,所以为了清楚起见,我发布了最初列出的页面代码。
<?php
include('includes/database.php'); ?>
<?php
//Create the select query
$query = "SELECT
weapon_types_martial.id,
weapon_types_martial.weapon_name,
weapon_types_martial.weapon_type,
weapon_types_martial.weapon_damage
FROM weapon_types_martial
ORDER BY weapon_name";
//Get results of query
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../favicon.ico">
<title>App Test | Weapons</title>
<!-- Bootstrap core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<style type="text/css"></style>
</head>
<body>
<div class="site-wrapper">
<div class="site-wrapper-inner">
<div class="cover-container">
<div class="masthead clearfix">
<div class="inner">
<h3 class="masthead-brand">Cover</h3>
<nav>
<ul class="nav masthead-nav">
<li><a href="index.php">Front Page</a></li>
<li class="active"><a href="weapons.php">Weapons</a></li>
<li><a href="armor.php">Armor</a></li>
<li><a href="consumables.php">Consumables</a></li>
</ul>
</nav>
</div>
</div>
<form action="#" method="post">
<table class="table">
<thead>Martial Weapon Name</thead>
<tr>
<th>
<select name="Choosen">
<?php
echo'<option>Select Weapon</option>';
//Check if at least one row is found
if($result->num_rows >0){
//Loop through results
while($row = $result->fetch_assoc()){
//Display weapon info
$output = $row['weapon_name'];
echo '<option>'.$output.'</option>';
}
}
?>
</select>
</th>
</tr>
</table>
<input class="btn btn-default" type="submit" name="submit" value="Generate">
<h3>Weapon</h3>
<table class="table table-striped">
<tr>
<th>Weapon Name</th>
<th>Weapon Type</th>
<th>Damage</th>
</tr>
<?php
$choose= "SELECT
weapon_types_martial.id,
weapon_types_martial.weapon_name,
weapon_types_martial.weapon_type,
weapon_types_martial.weapon_damage
FROM weapon_types_martial";
$result = $mysqli->query($choose) or die($mysqli->error.__LINE__);
if(isset($_POST['submit'])){
$selected_weapon = $_POST['Choosen'];
while($list = $result->fetch_assoc()){
//Display weapon
$show ='<tr>';
$show .='<td>'.$list['weapon_name'].'</td>';
$show .='<td>'.$list['weapon_type'].'</td>';
$show .='<td>'.$list['weapon_damage'].'</td>';
$show .='</tr>';
//Echo output
echo $show;
}
}
?>
</form>
</div>
</div>
</div>
</div>
</div>
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="../../assets/js/vendor/jquery.min.js"><\/script>')</script>
<script src="../../dist/js/bootstrap.min.js"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="../../assets/js/ie10-viewport-bug-workaround.js"></script>
上述代码的结果显示在上面的屏幕截图中。下拉列表应该显示正确的武器列表,但我的想法是我希望能够选择其中一种武器,点击生成,它只在下表中显示一种选择的武器。现在它显示了列表中的所有武器,而不仅仅是选择的武器。
【问题讨论】:
标签: php mysql database html-table