列表框在每个元素下方显示空白行空格。宽度也增加但仍出现空白行。我已附上截图供参考。我已附上代码文件。我正在使用一个函数从下拉列表中选择记录。下拉列表正在运行,但空白记录显示在屏幕截图中显示的每个值下方。
<?php
$selected = '';
function get_options($select) {
$categories = array('Information Technology' => 1, 'Management' => 2);
$options = '';
while (list($k, $v) = each($categories)) {
if ($select == $v) {
$options .= '<option value="' . $v . '" selected>' . $k . '<option>';
} else {
$options .= '<option value="' . $v . '" >' . $k . '<option>';
}
}
return $options;
}
require_once('dbconnect.php');
if (isset($_POST['categories'])) {
$selected = $_POST['categories'];
echo $selected;
}
if ($selected == 1) {
$selectedcat = 'Information Technology';
$selectsql = "SELECT * FROM courses where ccategory='$selectedcat'";
} else
if ($selected == 2) {
$selectedcat = 'Management';
$selectsql = "SELECT * FROM courses where ccategory='$selectedcat'";
} else {
$selectsql = "SELECT * FROM courses";
}
//require_once('dbconnect.php');
include('header-basic-light.php');
//$selectsql="SELECT * FROM courses";
$res = (mysqli_query($con, $selectsql));
if (!mysqli_query($con, $selectsql)) {
die(mysqli_error($con));
}
mysqli_close($con);
//header('Location:index.php');
?>
<HTML>
<head>
<title>"View Information"</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="categories">Select the Category : </label>
<select name="categories" style="width:250px;" onchange="this.form.submit();">
<?php echo get_options($selected); ?>
</select>
</form>
<h2>View Information</h2>
<table class="table">
<tr>
<th>#</th>
<th>cname</th>
<th>start_date</th>
<th>duration</th>
<th>Remarks</th>
<th>Options</th>
</tr>
<?php
while ($r = mysqli_fetch_assoc($res)) {
?>
<tr>
<td><?php echo $r['cno']; ?></td>
<td><?php echo $r['cname']; ?></td>
<td><?php echo $r['start_date']; ?></td>
<td><?php echo $r['duration']; ?></td>
<td><?php echo $r['remarks']; ?></td>
<?php
if ($r['ccategory'] == 'Information Technology') {
$catnum = 1;
}
if ($r['ccategory'] == 'Management') {
$catnum = 2;
}
?>
<td><a href="loadpage.php?id=<?php echo $catnum; ?>">Details  </a>
</tr>
<?php
}
?>
</table>
</body>
</html>
答案 0 :(得分:2)
第一名:您错过了选项关闭代码</option>
$options.='<option value="'.$v.'" selected>'.$k.'</option>';
第二名:尝试使用预准备语句来避免sql注入。