1

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

2
  • 3
    Please stop writing new code with the ancient mysql_* functions. They are no longer maintained and community has begun the deprecation process . Instead you should learn about prepared statements and use either PDO or MySQLi. If you care to learn, here is a quite good PDO-related tutorial. Commented Apr 20, 2012 at 19:23
  • thx for that, I will def look into this once i get this project done. Am under big time pressure. What would this one statement then look like, for example ? Commented Apr 20, 2012 at 19:26

2 Answers 2

3

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>";
Sign up to request clarification or add additional context in comments.

4 Comments

any idea how I could return a dimensional array before printing them into a list ? My functions to do with DB connections, are in different files tyhan the file where I will use it !
so i first need to create an array I can work with, pass it to a different file, then print them there as a list
apart from the fact that when looking at it, I struggel understanding the loops to print it. Becasue the q_code, value and title variable in the loops is made up, but still it prints the items correctly. I guess the first loop, indicates the first part of teh array, second loop is for second part of array !?
yeah those var names are just designated to refer to the item the pointer is currently on. foreach($array as $key => $value)
1

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.

1 Comment

thx for the answer and the links !! really appreciated, I will def look through the site on occasions !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.