Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

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
0
MrFahad
Asked:
MrFahad
  • 6
  • 5
  • 2
2 Solutions
 
Graceful_PenguinCommented:
Just query the database for the field and then step through the result and output it into the dropdown.
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

0
 
striker46Commented:
Penguin, I think you forgot closing the </option> in your code...
0
 
MrFahadAuthor Commented:
Penguin it is only display 1st letter of the PropertyName
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
MrFahadAuthor Commented:
any help please :(?

here is a screen shot
ss.jpg
0
 
striker46Commented:
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

0
 
Graceful_PenguinCommented:
@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?
0
 
MrFahadAuthor Commented:
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
0
 
striker46Commented:
Could you please post the outputted HTML code of the dropdown menu? From the first screenshot, with the fetched results.
0
 
MrFahadAuthor Commented:
<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>
0
 
striker46Commented:
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

0
 
striker46Commented:
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

0
 
striker46Commented:
Doh... how stupid of me, forgot a " now

Will review it better next time, at first ;)


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

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

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

  • 6
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now