Jump to content

[SOLVED] Create php array from mysql select statement


snorky

Recommended Posts

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.