Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I generate a drop down from a database table?

My drop down is blank as shown below:

Blank Drop Down

I tried to generate this drop down using the code below:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<?php
$mysqli = NEW MYSQLI('localhost', 'root', '','staff');
$resultSet = $mysqli->query("Select tripid from college");

?>

<label for="">Trip ID</label>
<p>
    <select name="tripid" id="tripid" style="width: 300px">
    <option value='-1' selected>Trip ID</option>
   
<php?
while ($rows = $resultSet->fetch_assoc()){
    $tripid = $rows['$tripid'];
    echo "<option value='$tripid'>$tripid</option>";
}
?>

    </select>

>Solution :

So it’s generating something but it’s apparently blank. It would be really helpful to see what the generated HTML looks like instead of a picture of an HTML element. The code is important here, not what it looks like.

However, I suspect it’s just rendering these literal strings:

<option value='$tripid'>$tripid</option>
<option value='$tripid'>$tripid</option>
<option value='$tripid'>$tripid</option>

I also suspect that $rows['$tripid'] should be $rows['tripid'] since it’s unlikely that you have key name with $ in it like a PHP variable.

In your PHP code you need to concatenate the HTML string with the PHP variables, like this:

while ($rows = $resultSet->fetch_assoc()){
    $tripid = $rows['tripid'];
    echo "<option value='" . $tripid . "'>" . $tripid . "</option>";
}

now it should render the actual variable content, like this:

<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading