3

I have a PHP script to populate drop down menu with results obtained from database the problem I am having is that only the last result is displayed in drop down menu. I recon it is because the while loop that gets all the results overwrites the variable that stores a string every time is runs.

I have tried to find a solution to fix it but ending up in a dark corner with no solution

Php Code:

$sql2 = "SELECT id, course, location FROM courses WHERE course LIKE '%Forex%' OR  course LIKE '%forex%'";
        $query2 = mysqli_query($link, $sql2);
            $opt = '<select id="Forex" name="Forex">';
            $opt1 = '<option value="">Select Forex Workshop</option>';



                while($result2 = mysqli_fetch_assoc($query2)){
                //I belief the $opt2 variable is overwritten every time the loop runs
                    $opt2 = '<option value="">'.$result2['course'].'</option>';
                    print_r($opt2);
                }
                $opt3 =  '</select>';
                return $opt.$opt1.$opt2.$opt3;  


}       

I might be wrong and the issue might be elswhere in the code but when i print_r($result2) all the correct results are there

1
  • 1
    $opt2 = '<option value=""'.$results2['course'].'</option>'; should be $opt2 .='<option value="">'.... So add a dot before =. Otherwise you will just receive the last value from Database. Commented Dec 19, 2013 at 9:52

3 Answers 3

3

Just add a .

$opt2 .= '<option value="">'.$result2['course'].'</option>';
      ^

Your variable is reinitializing, it should be concatenated.

So, the final code should be:

$opt2 = '';
while($result2 = mysqli_fetch_assoc($query2)){
                //I belief the $opt2 variable is overwritten every time the loop runs
                    $opt2 = '<option value="">'.$result2['course'].'</option>';
                    print_r($opt2);
                }
                $opt3 =  '</select>';
                return $opt.$opt1.$opt2.$opt3;
Sign up to request clarification or add additional context in comments.

4 Comments

lool simple as thats great I can see all the results now but still getting a Notice Warning " Notice: Undefined variable: opt2 in C:\xampp\htdocs\opes\course.php on line 21"
@Tomazi, if this works for you, please mark my answer as solution.
The error is gone now i fixed it by initializing the $opt2 variable $opt2 = ''; trying to accept your answer but is cant as soon i i can i will because you were first. Sometimes it is scary ho simple a solution can be :)
Actually he wasn't first, I posted 24 seconds before him. ;)
2

Yes, you need to append each value:

$opt2 .= '<option value="">'.$result2['course'].'</option>';

You should also initialise $opt2 before you start the loop.

$opt1 = '<option value="">Select Forex Workshop</option>';
$opt2 = "";

Comments

1

You need to concatenate the each value with existing variable in the loop

**$opt2 .= '<option value="">'.$result2['course'].'</option>';**

Comments

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.