Skip to content Skip to sidebar Skip to footer

Php Mysql Displaying Results As Drop Down List

I am trying veru hard, to get results from a database to be displayed as a drop down list. Basically I am fetching the foreign key, its unique code and its name/title. I need to di

Solution 1:

Assuming all you're trying to do is echo these rows into a dropdown:

$sql = "SELECT quali_code, title FROM `qualifications` ORDER BY title ";
$result = mysql_query($sql, $this->connection);
while($r=mysql_fetch_array($result))
{
$quali_code = $r['quali_code'];
$result_array[$quali_code][] = $r['title'];
}


echo"<select name='x'>";
foreach($result_arrayas$q_code => $value)
{
    foreach($valueas$title) {
    echo"<option value='" . $q_code . "'>" . $title . "</option>";
    }
}
echo"</select>";

Solution 2:

It's been awhile since I've used PHP, however, I remember this is how I printed out columns from a MySQL query. Use $r["columnName"] in the while loop. There may be semantic erros but this should give you a pretty good idea.

echo"<select>";
 while($r = mysql_fetch_array($result))
 {
    echo"<option value=\".$r['quali_code'].\">.$r['title'].</option>";
 }
 echo"</select>";

I used http://www.tizag.com/phpT/ to teach myself PHP. They have many great tutorials. Here's the link for a MySQL/PHP tutorial on Tizag: http://www.tizag.com/mysqlTutorial/.

Happy coding.

Post a Comment for "Php Mysql Displaying Results As Drop Down List"