156418 Posted October 6, 2006 Share Posted October 6, 2006 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:[code]<?php//we need to connect to the database sorequire('db/db_connect.php');?> <?php $query = "SELECT * FROM EventDates WHERE EventID = '22'"; $results = mysql_query($query) or die(mysql_error()); ?>[/code]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! Quote Link to comment https://forums.phpfreaks.com/topic/23179-populate-drop-down-menu-from-query/ Share on other sites More sharing options...
printf Posted October 6, 2006 Share Posted October 6, 2006 A simple example...[code]<?php//we need to connect to the database sorequire ( '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';}?>[/code]fix the closing </option>, thanks HuggieBearme! Quote Link to comment https://forums.phpfreaks.com/topic/23179-populate-drop-down-menu-from-query/#findComment-105035 Share on other sites More sharing options...
156418 Posted October 6, 2006 Author Share Posted October 6, 2006 Thanks, populating the menu as expected. 8) Quote Link to comment https://forums.phpfreaks.com/topic/23179-populate-drop-down-menu-from-query/#findComment-105044 Share on other sites More sharing options...
HuggieBear Posted October 6, 2006 Share Posted October 6, 2006 There's a slight error there...Change this:[code]echo "<option value='" . $row['some_column'] . "'>" . $row['some_other_column'] . "</a>";[/code]To this:[code]echo "<option value='" . $row['some_column'] . "'>" . $row['some_other_column'] . "</option>";[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/23179-populate-drop-down-menu-from-query/#findComment-105047 Share on other sites More sharing options...
printf Posted October 6, 2006 Share Posted October 6, 2006 [quote author=HuggieBear link=topic=110696.msg448020#msg448020 date=1160144424]There's a slight error there...Change this:[code]echo "<option value='" . $row['some_column'] . "'>" . $row['some_other_column'] . "</a>";[/code]To this:[code]echo "<option value='" . $row['some_column'] . "'>" . $row['some_other_column'] . "</option>";[/code]RegardsHuggie[/quote]hehehe, did I do that, that what I get for refusing a link loop, LoL!Thanks lookout, [b]HuggieBear[/b]me! Quote Link to comment https://forums.phpfreaks.com/topic/23179-populate-drop-down-menu-from-query/#findComment-105061 Share on other sites More sharing options...
156418 Posted October 6, 2006 Author Share Posted October 6, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/23179-populate-drop-down-menu-from-query/#findComment-105113 Share on other sites More sharing options...
156418 Posted October 6, 2006 Author Share Posted October 6, 2006 So I've got the first one working with Event DescriptionBut 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.[code] <?php//we need to connect to the database sorequire ( '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>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/23179-populate-drop-down-menu-from-query/#findComment-105259 Share on other sites More sharing options...
HuggieBear Posted October 8, 2006 Share Posted October 8, 2006 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:[code=php:0]echo "<option value='" . $row['EventDate'] . "'>" . $row['EventDate'] . "</a>";[/code]to:[code=php:0]echo "<option value='" . $row['EventDate'] . "'>" . $row['EventDate'] . "</option>";[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/23179-populate-drop-down-menu-from-query/#findComment-106008 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.