Jump to content


Photo

Populate Drop Down Menu from Query


  • Please log in to reply
7 replies to this topic

#1 156418

156418
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 06 October 2006 - 01:48 PM

This is probably a simple request, but I cant seem to find an answer - all I can find are answers that are doing more sophisticated things.

I'm running this query:

<?php
//we need to connect to the database so
require('db/db_connect.php');
?> 
<?php
	$query = "SELECT * FROM EventDates WHERE EventID = '22'";
	$results = mysql_query($query)
	 or die(mysql_error());
	 
	 ?>

Which works no problem, as I can get the results to display as Text.

However I'd like to take the results and put them into a Drop Down Menu to pass on to the next part of my form.

Could anyone adivse how I take the results and put the into the drop down, I cant seem to get the code right, and just get the php partly displayed in the box!

#2 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 06 October 2006 - 02:06 PM

A simple example...

<?php

//we need to connect to the database so

require ( 'db/db_connect.php' );

$result = mysql_query ( 'SELECT * FROM EventDates WHERE EventID = 22;' ) or die ( 'Query Error: ' . mysql_error () );

if ( mysql_num_rows ( $result ) > 0 )
{
	echo "<select name='some_select_name'>";

	while ( $row = mysql_fetch_array ( $result, MYSQL_ASSOC ) )
	{
		echo "<option value='" . $row['some_column'] . "'>" . $row['some_other_column'] . "</option>";
	}

	echo '</select>';
}
else
{
	echo 'can not create select box, no results found';
}

?>

fix the closing </option>, thanks HuggieBear


me!

#3 156418

156418
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 06 October 2006 - 02:19 PM

Thanks, populating the menu as expected.

8)

#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 06 October 2006 - 02:20 PM

There's a slight error there...

Change this:
echo "<option value='" . $row['some_column'] . "'>" . $row['some_other_column'] . "</a>";

To this:
echo "<option value='" . $row['some_column'] . "'>" . $row['some_other_column'] . "</option>";

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#5 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 06 October 2006 - 02:39 PM

There's a slight error there...

Change this:

echo "<option value='" . $row['some_column'] . "'>" . $row['some_other_column'] . "</a>";

To this:
echo "<option value='" . $row['some_column'] . "'>" . $row['some_other_column'] . "</option>";

Regards
Huggie


hehehe, did I do that, that what I get for refusing a link loop, LoL!


Thanks lookout, HuggieBear

me!

#6 156418

156418
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 06 October 2006 - 04:22 PM

Many thanks, I hadn't come across the error as my first page I'm using that on only has one option in the table, but the next one will need multiple options

#7 156418

156418
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 06 October 2006 - 09:05 PM

So I've got the first one working with Event Description

But the next one is event date, there are 5 items listed, and the drop down populates ok, however it doesn't pass it on in the POST. I.e the EventDescription, Password and EmailAddress are passing on find, but EventDate which is being seleted at this point isnt.


<?php

//we need to connect to the database so

require ( 'db/db_connect.php' );

$result = mysql_query ( 'SELECT * FROM AvailableDates WHERE EventID = 22;' ) or die ( 'Query Error: ' . mysql_error () );

if ( mysql_num_rows ( $result ) > 0 )
{
	echo "<select name='AvailableDates'>";

	while ( $row = mysql_fetch_array ( $result, MYSQL_ASSOC ) )
	{
		echo "<option value='" . $row['EventDate'] . "'>" . $row['EventDate'] . "</a>";
	}

	echo '</select>';
}
else
{
	echo 'can not create select box, no results found';
}

?>

       <input type="hidden" name="EmailAddress" value="<?php echo $_POST['EmailAddress']; ?>">
        <input type="hidden" name="Password" value="<?php echo $_POST['Password']; ?>">
	   <input type="hidden" name="EventDescription" value="<?php echo $_POST['EventDescription']; ?>">
	  
	   
	   <input type="submit" name="Submit" value="Submit"></form>


#8 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 08 October 2006 - 10:34 PM

Once the page with the date dropdown appears, view the source and paste it here, that way we can see what values are appearing... Also, post the code for the page that's processing the values.

You need to change the line I pointed out earlier again...

change:
echo "<option value='" . $row['EventDate'] . "'>" . $row['EventDate'] . "</a>";
to:
echo "<option value='" . $row['EventDate'] . "'>" . $row['EventDate'] . "</option>";


Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users