php - 列表框在每个元素下方显示空白行空间

标签 php mysql html

列表框在每个元素下方显示空白行空间。宽度也增加了,但仍然出现空行。我已附上屏幕截图以供引用。我已附上代码文件。我正在使用一个函数从下拉列表中选择记录。下拉列表正常工作,但屏幕截图中显示的每个值下方显示空白记录。

<?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&nbsp&nbsp</a>
                        </tr>
                        <?php
                    }
                    ?>
                </table>
                </body>
                </html>

Listbox screenshot

最佳答案

第一:您在选项结束标记 </option> 中错过了正斜杠

$options.='<option value="'.$v.'" selected>'.$k.'</option>';

第二:尝试使用prepared语句来避免sql注入(inject)。

关于php - 列表框在每个元素下方显示空白行空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45912185/

相关文章:

php - Laravel 5 如何以正确的方式在不同的数据库中运行测试?

php - YouTube Api(V2.0 或 3.0)获取给定 channel url 的 channel 信息

php - 在 PHP 中打印多个反序列化数组

html - 在 Ajax 调用返回之前重定向会导致问题吗?

javascript - 在 WordPress 主题中添加 Google 字体的函数出错

php - 将 PHP Doc 注释解析为数据结构

php - 修改 mySQL 查询以在使用 ASC 时在末尾显示空行,而不是在顶部

php - 无符号非空 mySql

jquery - 棘手的 iframe View

html - 如何禁用 Angular Material 中的第二个 mat-list-item