Jump to content

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

}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.