Jump to content

[SOLVED] Fill select box with query


EdwardJ

Recommended Posts

Hi,

 

I'm trying to fill a select box with mysql results from a query and have come up with the following code:

<select name="userlevel">
	<?php 
		$query = "SELECT userlevel FROM user_levels ORDER BY userlevel_id";
		$result = mysql_query($query);
		while ($row = mysql_fetch_array($result)){
			echo "<option value= .$row .> </option>";
		}
		echo "</select>";
	?>

 

So far the select box shows empty, and I do have at least one field in the table.

Also if there is a way to do this with oop, please let me know.

Thanks for your help.

Link to comment
https://forums.phpfreaks.com/topic/134418-solved-fill-select-box-with-query/
Share on other sites

<select name="userlevel">
	<?php 
		$query = "SELECT * FROM user_levels ORDER BY userlevel_id";
		$result = mysql_query($query);
		while ($row = mysql_fetch_assoc($result)){
			echo "<option value=\"$row[userlevel]\"> </option>";
		}
		echo "</select>";
	?>

 

try that

Do you mean it is empty with no drop down options or are there drop down options just empty ones.

 

anyway here is a code fix

<select name="userlevel">
      <?php 
         $query = "SELECT userlevel FROM user_levels ORDER BY userlevel_id DESC";
         $result = mysql_query($query);
         while ($row = mysql_fetch_array($result))
         {
            echo "<option value= \"".$row['userlevel']."\">".$row['userlevel']."</option>";
         }
         echo "</select>";
      ?>

For an optimized query you should really only SELECT the fields you need like above, not *.

@Maq, depends on the size of the table and the amount of rows it has.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.