Jump to content

[SOLVED] Create php array from mysql select statement


snorky

Recommended Posts

loop through the results, and add each row to an array

 

$query = "select distinct bldg from main order by bldg";
$result = mysql_query($query);

$my_array = array();

while ($row = mysql_fetch_assoc($result)){
$my_array[] = $row['bldg'];
}

THX !!

 

I had to change one line to make it work:

$my_array[] = $row['bldg'];

became

$my_array = $row['bldg'];

Without that change the code didn't work.

 

What I did with your solution is to create a 'dynamic' menu using HTML <select><option>...

 

The menu offers all possible values for the bldg field, even if values are added or lost - and the list is always ordered in whatever way the query calls:

 

$db=mysql_select_db("roster",$link); 

$query = "select distinct bldg from main where bldg !='' order by bldg";
$result = mysql_query($query);
$my_array = array();
$line=0;      				// initialize a counter for while loop
echo "<select type='text' name='titleb' value='' />";
	while ($row = mysql_fetch_assoc($result))
	{
		$line++;
		$my_array = $row['bldg'];
		echo "<option value =" . "'$my_array'" . ">" . $my_array . "</option>";
	}
echo "</select>";	

 

No doubt there are more elegant ways to create that menu, but the concept is simple and I'm already thinking about other ways to use it.

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.