richrock Posted April 14, 2009 Share Posted April 14, 2009 Hi, having a really strange problem with a select result... here's the code: <select name="city" id='inputtext' onchange="this.form.submit();" class="inputbox"> <option value="">-- Select City --</option> <?php while ($row_city=mysql_fetch_array($rcity)) { echo '<option value="'.$row_city['city'].'">'.$row_city['city'].'</option>'; //echo '<option value="'.$row_city['city'].'"'.($row_city['city']==$city ? ' selected' : '').'>'.$row_city['city'].'</option>'; } ?> </select> What happens is if there is more than one result in the mysql query, the select populates with all the results. If there is only one result in the mysql query, then it doesn't show at all. I have checked this as I am using mysql_num_rows and have tested it. I can't figure why this is. Any ideas? And here's the mysql bit : $getcity = "SELECT DISTINCT city FROM yearbook "; if ($src_country == !NULL) { $getcity .= "WHERE country = '".$src_country."' "; } $getcity .= "ORDER BY city ASC"; $rcity = mysql_query($getcity) or die(mysql_errno() . mysql_error()); $row_city = mysql_fetch_assoc($rcity); $tRows_city = mysql_num_rows($rcity); The $src_country bit is for the select to filter the results if someone has selected a country, and I'm doing the same on the country select if they select a city first... Link to comment https://forums.phpfreaks.com/topic/153997-solved-select-single-result-not-showing/ Share on other sites More sharing options...
MasterACE14 Posted April 14, 2009 Share Posted April 14, 2009 change this... if ($src_country == !NULL) { to this... if ($src_country != NULL) { delete this line as it isn't needed... $row_city = mysql_fetch_assoc($rcity); Link to comment https://forums.phpfreaks.com/topic/153997-solved-select-single-result-not-showing/#findComment-809427 Share on other sites More sharing options...
richrock Posted April 14, 2009 Author Share Posted April 14, 2009 I originally had ($src_country != NULL)... can't think why I changed it... Anyhoo, works now. Thanks a lot! Case Closed. Link to comment https://forums.phpfreaks.com/topic/153997-solved-select-single-result-not-showing/#findComment-809434 Share on other sites More sharing options...
richrock Posted April 14, 2009 Author Share Posted April 14, 2009 Okay, it works fine, but I've discovered a new issue (ah the joys of programming ) I have in the DB the cities (city), and they don't always have a city allocated, so the entry is empty. The select now defaults to the blank entry, instead of the --Select City-- option... so I get something like this in the output: <select> <option value = "">--Select City--</option> <option value = "" selected></option> <option value = "London">London</option> <option value = "Manchester">Manchester</option> (etc...) </select> But it should either be --Select City-- or the city I have chosen... I can't remove the blank entries, but don't wnat them selected when I choose my country. TIA, Richard Link to comment https://forums.phpfreaks.com/topic/153997-solved-select-single-result-not-showing/#findComment-809493 Share on other sites More sharing options...
MasterACE14 Posted April 14, 2009 Share Posted April 14, 2009 try changing this line... <?php $getcity .= "WHERE country = '".$src_country."' "; to... <?php $getcity .= "WHERE country = '".$src_country."' && `city`!='' "; Link to comment https://forums.phpfreaks.com/topic/153997-solved-select-single-result-not-showing/#findComment-809499 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.