Link to home
Start Free TrialLog in
Avatar of MrFahad
MrFahad

asked on

How to create a dynamic drop down menu from a feild in my database

Hello, i have a database for real estate website. i want to want to make a dynamic drop-down menu that will show the records from a feild in my database's table.

the feild name is PropertyName.

Thank you
SOLUTION
Avatar of Graceful_Penguin
Graceful_Penguin
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Penguin, I think you forgot closing the </option> in your code...
Avatar of MrFahad
MrFahad

ASKER

Penguin it is only display 1st letter of the PropertyName
Avatar of MrFahad

ASKER

any help please :(?

here is a screen shot
ss.jpg
What's the problem with Penguin's code? Apart that there's a missing >?
echo '<SELECT>';
$result = mysql_query('SELECT PropertyName FROM tablename');
foreach (mysql_fetch_array($result) as $property)
{
  echo '<option value="'.$property['PropertyName'].'">'.$property['PropertyName'].'</option>';
}
echo '</SELECT>';

Open in new window

@striker46 : Thanks for the missing >

@MrFahad : This piece of code should display exacly what is in the field of the database. Just to make sure we are talking about a field not a row?
Avatar of MrFahad

ASKER

no it's the row ( record )

here is the screen shot of my DB i pointed with any arrow what i want to disply
ss2.jpg
Could you please post the outputted HTML code of the dropdown menu? From the first screenshot, with the fetched results.
Avatar of MrFahad

ASKER

<label for="PropertyName"><em>Please enter a unique property name that the customer bought:</em></label>
                    <SELECT><option value="9">9</option><option value="9">9</option></SELECT>
That's weird, it retrievied twice a same row? I see the code has two options "9" but in the table you showed in the 2nd screenshot there's just 1 row, isn't there?

Try the code below
<?php
 
$sql = "SELECT PropertyName 
        FROM properties;
 
$result = mysql_query($sql);
 
while ($row = mysql_fetch_assoc($result)) {
 
echo '<option value="'.$row["PropertyName"].'">'.$row["PropertyName"].'</option>';
 
}
 
 
?>

Open in new window

Sorry, forgot the <SELECT>



echo "<SELECT>";
 
$sql = "SELECT PropertyName 
        FROM properties;
 
$result = mysql_query($sql);
 
while ($row = mysql_fetch_assoc($result)) {
 
echo '<option value="'.$row["PropertyName"].'">'.$row["PropertyName"].'</option>';
 
}
 
echo "</SELECT>";

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MrFahad

ASKER

Worked Great Striker46, I'm also giving points for penguin Thanks guys great work.