Demir Oral
Demir Oral

Reputation: 65

While loop only creates one dropdown box per client (PHP/MySQL)

I am attempting to use two queries to set up multiple dropdown menus per doctor. So far this is working, however it unfortunately pulls only the item.

I would like to have all of the 'expertises' available within the dropdown (and the expertise IDs as the option values).

I have set up the below while loop, however I have been unsuccessful in producing a dropdown for each Client_ID.

The first query (below) pulls the client ID from the area

$client_id = $_GET['doc_id'];
$physician_id_expertise = $client_id;

$doctor_query = 'SELECT * ';
$doctor_query.= 'FROM `primarycare_expertise` ';
$doctor_query.= 'WHERE `physician_id`=' . $client_id . ' ';
$doctor_result = mysql_unbuffered_query( $doctor_query );

The while loop incorporates the second query, which sets up the dropdown (pulling in all of the 'expertise' values).

while ($doctor_row = mysql_fetch_array( $doctor_result ))
{
    echo "<select name='doc_id_expertise_" . $doctor_row['pe_id'] . "' id='doc_id_expertise_" . $doctor_row['pe_id'] . "'>";

    $expert_query = 'SELECT * FROM `expertise` ';
    $expert_result = mysql_unbuffered_query( $expert_query );

    while ($expert_row = mysql_fetch_array( $expert_result )){
        if($doctor_row['expertise_id'] == $expert_row['expertise_id'])
        {
            $selected = "selected";
        } else {
            $selected = "";
        }
        echo "<option $selected value=" . $expert_row['expertise_id'] . ">" . $expert_row['expertise'] . "</option>";
    }

    echo "</select>";
    echo "<br />";
}

If any of you could help me out, I would be very appreciative! Thank you for your time!

Upvotes: 0

Views: 636

Answers (2)

Coert Metz
Coert Metz

Reputation: 892

The mysql package is deprecated BTW. Consider adopting to the mysqli extension instead.

http://www.php.net/manual/en/book.mysqli.php

Upvotes: 0

Mikhail Vladimirov
Mikhail Vladimirov

Reputation: 13890

You are sending new query while iterating through unbuffered result of previous query. This does not work. Try using mysql_query instead of mysql_unbuffered_query for the first query like this:

$doctor_result = mysql_query (
    "SELECT * FROM primarycare_expertise WHERE physician_id='$client_id'");

For subsequent queries you could still use mysql_unbuffered_query because they do not overlap.

See the following page for details.

Upvotes: 1

Related Questions