2012-04-16 8 views
1

データベースの値をドロップダウンリストに入力したいとします。PHPがSQLデータを含むドロップダウンリスト

<?php 
require 'conn.php'; 

$filter=mysql_query("select distinct fuel_type from car"); 
while($row = mysql_fetch_array($filter)) { 
$options ="<option>" . $row['fuel_type'] . "</option>"; 

$menu="<form id='filter' name='filter' method='post' action=''> 
    <p><label>Filter</label></p> 
    <select name='filter' id='filter'> 
     " . $options . " 
    </select> 
</form>"; 

echo $menu; 
} 
?> 

問題は、値が1つのリストではなく2つのリストが得られることです。

drop down list

答えて

2

あなたのループから$menuを削除する必要があります。

<?php 
require 'conn.php'; 

$options = ''; 
$filter=mysql_query("select distinct fuel_type from car"); 
while($row = mysql_fetch_array($filter)) { 
    $options .="<option>" . $row['fuel_type'] . "</option>"; 
} 

$menu="<form id='filter' name='filter' method='post' action=''> 
    <p><label>Filter</label></p> 
    <select name='filter' id='filter'> 
     " . $options . " 
    </select> 
</form>"; 

echo $menu; 

?> 
+0

が行われ、今私は一つの値だけを取得します。 'petrol' –

+1

whileループに' .'がありません。コードをコピー&ペーストしてもう一度やり直してください。 –

+0

確かに、それを見ていませんでした。 –

2

echo $menu;があなたのループの外でなければなりません教えてください。

1
<?php 
require 'conn.php'; 

$filter=mysql_query("select distinct fuel_type from car"); 
$menu=" 
<form id='filter' name='filter' method='post' action=''> 
    <p><label>Filter</label></p> 
    <select name='filter' id='filter'>"; 

// Add options to the drop down 
while($row = mysql_fetch_array($filter)) 
{ 
    $menu .="<option>" . $row['fuel_type'] . "</option>"; 
} 

// Close menu form 
$menu = "</select></form>"; 

// Output it 
echo $menu; 
?> 
関連する問題