mackin Posted February 1, 2012 Share Posted February 1, 2012 HI - I am dynamically populating a drop down form list from the table 'hotels', and the 'est_town' column. It is supposed to only list each town once, but several towns appear multiple times. Am I using the DISTINCT command correctly and if so, what can i do to stop multiple appearances of towns? $query_hotels_select = "SELECT DISTINCT est_town FROM hotels ORDER BY est_town ASC"; $hotels_select = mysql_query($query_hotels_select, $contractors) or die(mysql_error()); $row_hotels_select = mysql_fetch_assoc($hotels_select); $totalRows_hotels_select = mysql_num_rows($hotels_select); FORM BIT <select name="digs" id="digs"> <?php do { ?> <option value="<?php echo $row_hotels_select['est_town']?>"<?php if (!(strcmp($row_hotels_select['est_town'], $row_hotels_select['est_town']))) {echo "selected=\"selected\"";} ?>><?php echo ucwords(strtolower($row_hotels_select['est_town']))?></option> <?php } while ($row_hotels_select = mysql_fetch_assoc($hotels_select)); $rows = mysql_num_rows($hotels_select); if($rows > 0) { mysql_data_seek($hotels_select, 0); $row_hotels_select = mysql_fetch_assoc($hotels_select); } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/256183-php-and-mysql-problem/ Share on other sites More sharing options...
AyKay47 Posted February 1, 2012 Share Posted February 1, 2012 while ($row_hotels_select = mysql_fetch_assoc($hotels_select)); $rows = mysql_num_rows($hotels_select); if($rows > 0) { mysql_data_seek($hotels_select, 0); $row_hotels_select = mysql_fetch_assoc($hotels_select); } this is your problem, you are using mysql_data_seek statically inside of a loop, this is a no no as it will consistently reset the result pointer to the first row. Typically, this function is used to return the current row pointer and display it. Really, all that is needed is this: if($hotels_select) { while ($row_hotels_select = mysql_fetch_assoc($hotels_select)) { $rows = mysql_num_rows($hotels_select); ?> <option value="<?php echo $row_hotels_select['est_town']?>"<?php if (!(strcmp($row_hotels_select['est_town'], $row_hotels_select['est_town']))) {echo "selected=\"selected\"";} ?>><?php echo ucwords(strtolower($row_hotels_select['est_town']))?></option> <?php } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/256183-php-and-mysql-problem/#findComment-1313314 Share on other sites More sharing options...
mackin Posted February 1, 2012 Author Share Posted February 1, 2012 thank u Quote Link to comment https://forums.phpfreaks.com/topic/256183-php-and-mysql-problem/#findComment-1313462 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.