Reputation: 287
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 display it as a drop down list, to be entered correctly into a new table
$sql = "SELECT quali_code, title FROM `qualifications` ";
$result = mysql_query($sql, $this->connection);
$result_array = array();
while($r=mysql_fetch_array($result))
{
$result_array[$r['quali_code']][] = $r;
}
return $result_array;
With that I would need to create a drop down list in HTMl/PHP to select the title from, but obviously storing the value. sort of <list name=array[title] value=array[code] >
At the moment I am already stuck at returning my SQl result in an array that works for me, and have no clue how I will then populate a dropdown list with the correct array results.
Sample code above on how i fetch the sql results and here how my array looks like:
Array ( [AAT] => Array ( [0] => Array ( [0] => AAT [quali_code] => AAT [1] => AAT qualification [title] => AAT qualification ) ) [A_lev] => Array ( [0] => Array ( [0] => A_lev [quali_code] => A_lev [1] => A levels [title] => A levels ) ) [HNC] => Array ( [0] => Array ( [0] => HNC [quali_code] => HNC [1] => Fdsc/HNC [title] => Fdsc/HNC ) ) [Lan_ISLT] => Array ( [0] => Array ( [0] => Lan_ISLT [quali_code] => Lan_ISLT [1] => EISLT Qualification [title] => EISLT Qualification ) ) [Lan_qua] => Array ( [0] => Array ( [0] => Lan_qua [quali_code] => Lan_qua [1] => Language qualification [title] => Language qualification ) ) [Nat_Dip] => Array ( [0] => Array ( [0] => Nat_Dip [quali_code] => Nat_Dip [1] => National Diploma [title] => National Diploma ) ) )
Bare in mind I am totally new to PHP. Any help would be appreciated. But this array looks useless to me, and am not sure how to populate a list. The file where I fetch the result / array, is not the location where I populate the array into a list.
Many thanks for any hints on this matter
Upvotes: 1
Views: 3080
Reputation: 1003
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_array as $q_code => $value)
{
foreach($value as $title) {
echo "<option value='" . $q_code . "'>" . $title . "</option>";
}
}
echo "</select>";
Upvotes: 3
Reputation: 444
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.
Upvotes: 1