Bradley99 Posted June 23, 2012 Share Posted June 23, 2012 So, I want the normal drop down like so. . . <select name=. . . . > <option value=1>1</option> <option value=2>2</option> <option value=3>3</option> but I wan't the options to come from my DB. Any help is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/264674-drop-down-select-via-php/ Share on other sites More sharing options...
Pikachu2000 Posted June 23, 2012 Share Posted June 23, 2012 Where are you stuck? Quote Link to comment https://forums.phpfreaks.com/topic/264674-drop-down-select-via-php/#findComment-1356484 Share on other sites More sharing options...
Bradley99 Posted June 23, 2012 Author Share Posted June 23, 2012 I'm stuck on how to actually get the options from the database, something I've never done before, I was thinking it would be similar to fetching and ordering articles or something like that? Quote Link to comment https://forums.phpfreaks.com/topic/264674-drop-down-select-via-php/#findComment-1356485 Share on other sites More sharing options...
Pikachu2000 Posted June 23, 2012 Share Posted June 23, 2012 Do you know how to run a SELECT query and echo the values in a while() loop? If so, that's pretty much all there is to it. You just add the appropriate html tags as the values are echoed. Quote Link to comment https://forums.phpfreaks.com/topic/264674-drop-down-select-via-php/#findComment-1356486 Share on other sites More sharing options...
Bradley99 Posted June 23, 2012 Author Share Posted June 23, 2012 I can run a SELECT query and echo the values, but not in a while() loop, any help on that would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/264674-drop-down-select-via-php/#findComment-1356487 Share on other sites More sharing options...
Bradley99 Posted June 23, 2012 Author Share Posted June 23, 2012 So here's how I tried it. . $eventname = mysql_fetch_object(mysql_query("SELECT * FROM events ORDER by event_id ASC")); while($row = mysql_fetch_array($eventname)); With <tr> <td class="subtableheader"><b>Event:</b></td> <td class="profilerow"><select name=event><option value=1><?php echo "$row"; ?></option></td> </tr> And i get error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in . . . on line 28 Line 28 is the "while($row = mysql. . . ) Quote Link to comment https://forums.phpfreaks.com/topic/264674-drop-down-select-via-php/#findComment-1356489 Share on other sites More sharing options...
Bradley99 Posted June 24, 2012 Author Share Posted June 24, 2012 I can get it to list events, but not in a drop down! Any help is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/264674-drop-down-select-via-php/#findComment-1356602 Share on other sites More sharing options...
Mahngiel Posted June 24, 2012 Share Posted June 24, 2012 [*] Construct your query [*] Check if query returned any results [*] If so, open your <select></ic] form [*] then construct a while loop with [ic]<option>s [*] Close your </select> Quote Link to comment https://forums.phpfreaks.com/topic/264674-drop-down-select-via-php/#findComment-1356611 Share on other sites More sharing options...
Bradley99 Posted June 24, 2012 Author Share Posted June 24, 2012 This is what I got, But it isn't working, If I just echo the results as text, it works, but when i try and do it as a select drop down, it shows none. <td class="profilerow"><select name=event> <? $select_event = mysql_query("SELECT * FROM events ORDER by id ASC"); while($the=mysql_fetch_object($select_event)){ } echo "<option value=1>$the->event</option>"; ?></td> Quote Link to comment https://forums.phpfreaks.com/topic/264674-drop-down-select-via-php/#findComment-1356679 Share on other sites More sharing options...
Pikachu2000 Posted June 24, 2012 Share Posted June 24, 2012 What shows up in the html source? Where's the closing </select> tag? You know you can't use value=1 for every option in a select field, right? Quote Link to comment https://forums.phpfreaks.com/topic/264674-drop-down-select-via-php/#findComment-1356681 Share on other sites More sharing options...
Bradley99 Posted June 24, 2012 Author Share Posted June 24, 2012 Yeah i missed the closing select tag, made no difference though. Here's the HTML source. . <tr> <td class="subtableheader"><b>Event:</b></td> <td class="profilerow"> <select name=event><option value=1></option></select></td> </tr> And I do know that but don't know how I'd change that? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/264674-drop-down-select-via-php/#findComment-1356684 Share on other sites More sharing options...
Pikachu2000 Posted June 24, 2012 Share Posted June 24, 2012 Your query is either failing, or returning an empty results set. Do you have error reporting set up to display errors to the screen? You also need to verify that your query not only executed, but also returned more than 0 records before attempting to use the data. Quote Link to comment https://forums.phpfreaks.com/topic/264674-drop-down-select-via-php/#findComment-1356688 Share on other sites More sharing options...
Bradley99 Posted June 24, 2012 Author Share Posted June 24, 2012 I sorted why it wasn't calling results, curly bracket in wrong place, but now it's displaying separate drop downs for each event Quote Link to comment https://forums.phpfreaks.com/topic/264674-drop-down-select-via-php/#findComment-1356691 Share on other sites More sharing options...
Pikachu2000 Posted June 24, 2012 Share Posted June 24, 2012 Keeping in mind you're using a loop to echo everything, why do you think that might happen? Quote Link to comment https://forums.phpfreaks.com/topic/264674-drop-down-select-via-php/#findComment-1356692 Share on other sites More sharing options...
Bradley99 Posted June 24, 2012 Author Share Posted June 24, 2012 Yeah it's echoing separate drop downs for each event, i stopped it doing that, but now it just shows 1 drop down with event id 1. appreciate the help Quote Link to comment https://forums.phpfreaks.com/topic/264674-drop-down-select-via-php/#findComment-1356693 Share on other sites More sharing options...
Drummin Posted June 24, 2012 Share Posted June 24, 2012 <select name="event"> <?php $select_event = mysql_query("SELECT id, event FROM events ORDER by id ASC"); while($the=mysql_fetch_object($select_event)){ echo "<option value=\"" .$the->id . "\">" .$the->event . "</option>\r"; } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/264674-drop-down-select-via-php/#findComment-1356701 Share on other sites More sharing options...
Bradley99 Posted June 25, 2012 Author Share Posted June 25, 2012 THATS IT!! Thanks Thanks to everyone actually! Quote Link to comment https://forums.phpfreaks.com/topic/264674-drop-down-select-via-php/#findComment-1356709 Share on other sites More sharing options...
Mahngiel Posted June 25, 2012 Share Posted June 25, 2012 <select name="event"> <?php $select_event = mysql_query("SELECT id, event FROM events ORDER by id ASC"); while($the=mysql_fetch_object($select_event)){ echo "<option value=\"" .$the->id . "\">" .$the->event . "</option>\r"; } ?> </select> You don't want an empty select if there are no options. <?php // Define query $query = mysql_query("SELECT id, event FROM events ORDER by id ASC"); // Ensure you have values returned if( mysql_num_rows( $query ) >= 1 ) { // Rows is more than 1, build select menu $dropdown = '<select name="horse" id="horse_drop">'; // Now build loop while( $item = mysql_fetch_assoc( $query ) { $dropdown .= '<option value="' . $item["value"] . '">' . $item["name"] . '</option>'; } // Close select menu $dropdown .= '</select>'; } else { // Error checking $dropdown = 'bad horsey!'; ?> // Wherever you want your select menu to be, you simply echo $dropdown; </select> Quote Link to comment https://forums.phpfreaks.com/topic/264674-drop-down-select-via-php/#findComment-1356710 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.