使用数据库php中的数组填充html下拉列表

时间:2014-06-27 05:29:26

标签: php html postgresql drop-down-menu

我试图在下面的代码中填写html下拉列表。我在这里搜索了其他问题并试过但是它对我不起作用?任何人都可以帮我找到我的错误吗?

<form  id="data" class="form-geofence" role="form" method="post" action="geofencedata.php">
                <select name="category_id">
                    <option value="">--- Select ---</option>
                    <?php 
                        include_once'./connectionusers.php';
                        $result=pg_query("SELECT * FROM geofence;");
                        while ($row = pg_fetch_array($result)) {
                    ?>
                    <option value="" ><?php echo $row["name"];?></option>
                    <?php 
                            }
                    ?>
                </select>

我单独使用了相同的php代码,它显示了名称

5 个答案:

答案 0 :(得分:0)

<form  id="data" class="form-geofence" role="form" method="post" action="geofencedata.php">
                <select name="category_id">
                    <option value="">--- Select ---</option>
                    <?php 
                        include_once'./connectionusers.php';
                        $result=pg_query("SELECT * FROM geofence;");
                        while ($row = pg_fetch_array($result)) {
                    ?>
                    <option value="<?php echo $row["name"];?>" ><?php echo $row["name"];?></option>
                    <?php 
                            }
                    ?>
                </select>

你错过了回应期权价值,这可能是你的问题。

答案 1 :(得分:0)

function category_dropdown($selected=""){

    $open_tag=$close_tag=$option_tag=$selected_class="";
    $open_tag="<select name='category_id' id='category_id'>";
    $option_tag.="<option value="">--- Select ---</option>";
    $close_tag="</select>";


    include_once('./connectionusers.php');
    $result=pg_query("SELECT * FROM geofence;");
    while ($row = pg_fetch_array($result)) {
        $selected_class=($selected==$row["name"])?" selected='selected' ":"";
        $option_tag.="<option value='".$row["name"]."' ".$selected_class.">".$row["name"]."</option>";
    }
    return $open_tag.$option_tag.$close_tag;
}

答案 2 :(得分:0)

试试这种

<select name="case_type" style="width:250px">
<option>select</option>
<?php
$sql="select case_type_code,case_type_name from case_type_master where display='Y' 
                      order by case_type_name asc";
 foreach($dbh->query($sql) as $row)
 {
$casetypecode=$row['case_type_code'];
if($case_type == $casetypecode)
            {
print "<option  value=".$row['case_type_code']."selected>".$row['case_type_name']
      . "</option>";
            }
       else
            {
            print "<option value=".$row['case_type_code'].">"
             .$row['case_type_name']."</option>";
    }
  }
     ?>

     </select>

我相信它有效。 :)

答案 3 :(得分:0)

这是您更正后的错误代码。

<form  id="data" class="form-geofence" role="form" method="post" action="geofencedata.php">
            <select name="category_id">
                <option value="">--- Select ---</option>
                <?php 
                    include_once'./connectionusers.php';
                    $result=pg_query("SELECT * FROM geofence;");
                    while ($row = pg_fetch_array($result)) {
                ?>
                <option value="echo $row["code"];" ><?php echo $row["name"];?></option>
                <?php 
                        }
                ?>
            </select>

答案 4 :(得分:0)

确保您的数据库连接正确,并使用以下代码

<form  id="data" class="form-geofence" role="form" method="post" action="geofencedata.php">
            <select name="category_id">
                <option value="">--- Select ---</option>
                <?php 
                    include_once'./connectionusers.php';
                    $result=pg_query("SELECT value FROM geofence;");
                    while ($row = pg_fetch_array($result)) {
                     echo "<option value='".$row['value']."'>".$row['value']."</option>";
                       }
                ?>

            </select>