CodeMama Posted July 31, 2009 Share Posted July 31, 2009 I have this but it doesn't work..pulls no results: //Show all restaurants that start with $letter not between "A" and "Z" if ($selectedLetter = "#") { $other = range('0','9'); $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '$other'"; $result = mysql_query($sql) or die(mysql_error()); } while($row = mysql_fetch_assoc($result)){ $name = $row['name']; printf( '<a href="view.php?ID=%s"><b>%s</b><br />%s<br /><br /></a>', $row['ID'], $row['name'], $row['address'] ); } Quote Link to comment https://forums.phpfreaks.com/topic/168324-help-with-using-a-range-in-a-query/ Share on other sites More sharing options...
Psycho Posted July 31, 2009 Share Posted July 31, 2009 Where's the query for the situation when the user selected a letter? No need to create all new functionality for the "#" option, just change the WHERE part of your query. Give this a try, I probably made some typos though. Echo out the query to make sure it is proper. The MySQL substr looks like it starts with 1 as the first character, not 0 as PHP does, but I didn't validate. <?php //Show all restaurants that start with $selectedLetter if ($selectedLetter != "#") { //Letter was selected $where = "name LIKE '%{$selectedLetter}'" } else { //Number was selected $where = "SUBSTRING(name,1,1) IN ('" . implode("','", range(0,9)) . "')"; } $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE $where"; $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($result)){ $name = $row['name']; printf('<a href="view.php?ID=%s"><b>%s</b><br />%s<br /><br /></a>', $row['ID'], $row['name'], $row['address'] ); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/168324-help-with-using-a-range-in-a-query/#findComment-887913 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.