Jump to content

Help with using a range() in a query?


CodeMama

Recommended Posts

:facewall: 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']
                                                                );
                                                            
                                                        }

Link to comment
https://forums.phpfreaks.com/topic/168324-help-with-using-a-range-in-a-query/
Share on other sites

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']
    );

}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.