2017-08-28 31 views
0

列表框顯示每個元素下面的空行空格。也增加了寬度,但仍然出現空行。我附上了截圖供參考。我附上了代碼文件。我正在使用一個函數從下拉列表中選擇記錄。下拉列表正在運行,但空白記錄顯示在屏幕截圖中顯示的每個值的下方。列表框顯示每個元素下面的空白行空格

<?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

+0

可以請你把一個var_dump($選項);在你的函數中返回$ options行並在這裏粘貼輸出? – flauntster

+0

另外,請確保你清理輸入變量,因爲這個腳本在當前狀態下容易受到sql注入的影響:) – flauntster

+0

_Suggestion:_迭代選項的更直接的方法是:'foreach($ categories as $ k => $ v)'而不是你過於複雜的while循環(它基本上只是一個效率低下的方法來模擬foreach)。 –

回答

2

1:你錯過了向前選項關閉標籤削減</option>

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

第二:嘗試用事先準備好的聲明,以避免SQL注入攻擊。

+0

這是一個錯誤!我花了一段時間找出:P –

+0

是的,我忽略了。 – yesganesh