padams Posted October 30, 2008 Share Posted October 30, 2008 I'm trying to populate a dropdown menu using data in a recordset I get from by querying my database. The data is being added to the menu, but for some reason an extra, blank, value is being inserted at the top. This means the menu appears blank when the user first sees it. If they click on it, the values appear, but it's not ideal. Site is http://humphreyb.stacnz.com/pages/bedlinen.php The recordset is reused several times on the same page, so I have used mysql_data_seek. I thought this would ensure the data would appear okay, but no! It works in Dreamweaver Live, but not when I test it in any live browser. <select name="styleID" class="dropdownboxstyles" id="styleID"> <?php do { ?> <option value="<?php echo $rsStyle['styleID']; ?>"><?php echo $rsStyle['style']; ?></option> <?php } while ($rsStyle = mysql_fetch_assoc($style_query)) ?> <?php mysql_data_seek($style_query, 0); ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/130799-initial-value-of-dropdown-menu-is-blank/ Share on other sites More sharing options...
trq Posted October 30, 2008 Share Posted October 30, 2008 Use a while loop not a do while. Quote Link to comment https://forums.phpfreaks.com/topic/130799-initial-value-of-dropdown-menu-is-blank/#findComment-678832 Share on other sites More sharing options...
padams Posted October 31, 2008 Author Share Posted October 31, 2008 Thanks, that works fine now. I don't quite understand why there would be a difference. I know that do while loops always execute at least once, but I thought given the syntax it would still work the same as a simple while loop. What's the reason for the difference? Quote Link to comment https://forums.phpfreaks.com/topic/130799-initial-value-of-dropdown-menu-is-blank/#findComment-679101 Share on other sites More sharing options...
JasonLewis Posted October 31, 2008 Share Posted October 31, 2008 My guess would be that the first run it has no data since mysql_fetch_assoc() has not been run yet. Quote Link to comment https://forums.phpfreaks.com/topic/130799-initial-value-of-dropdown-menu-is-blank/#findComment-679120 Share on other sites More sharing options...
Daniel0 Posted October 31, 2008 Share Posted October 31, 2008 Thanks, that works fine now. I don't quite understand why there would be a difference. I know that do while loops always execute at least once, but I thought given the syntax it would still work the same as a simple while loop. What's the reason for the difference? The difference is the logic flow: while: 1) Is condition true? 2) If yes, execute block and start over. If no, terminate loop do-while: 1) Execute block 2) If condition is true, start over. Otherwise terminate loop. Quote Link to comment https://forums.phpfreaks.com/topic/130799-initial-value-of-dropdown-menu-is-blank/#findComment-679141 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.