Jump to content

[SOLVED] Adding "OTHER" to Alphabetical Pagination Script?


CodeMama

Recommended Posts

I have a working pagination script that makes links A-Z but I need to add an "other" field for record names that may not start with a letter...how can I do this?

 

The script so far:

 <?php
                                                       
                                                        $letter = isset($_GET['letter']) ? $_GET['letter'] :"A";


                                                       
                                                        //alphabetical pagination links
                                                        echo '<div align="center"><b>';
                                                        foreach(range('A','Z') as $c){
                                                          ($letter == $c)
                                                            ? printf('%s&nbsp',$c)
                                                            : printf('<a href="?letter=%s">%s</a> ',$c,$c);
                                                        }
                                                    
                                                        echo "</b><br></div><p>";
                                                        
                                                        //Show all restaurants that start with $letter
                                                        $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '{$letter}%'";
                                                        $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']
                                                                );
                                                            
                                                        }
                                                      
                                                      echo $row;
                                                        
                                                        //this outputs all restaurants
                                                        $letter = isset($_GET['letter']) ? $_GET['letter'] : "A";
                                                        
                                                          //Show all restaurants that start with $letter
                                                          /*
                                                          $sql = "SELECT * FROM restaurants";
                                                          $result = mysql_query($sql) or die(mysql_error());
                                                          while($row = mysql_fetch_assoc($result)){
                                                            printf('<div align="left" width="100"><b>%s</b><br>%s</br>%s</br></div>',$row['name'],$row['address'],$result['cviolations']);
                                                          }
                                                          */
                                                      ?>

give this a try:

 <?php
                                                       
                                                        $letter = isset($_GET['letter']) ? $_GET['letter'] :"A";


                                                       
                                                        //alphabetical pagination links
                                                        echo '<div align="center"><b>';
                                                        foreach(range('A','Z') as $c){
                                                          ($letter == $c)
                                                            ? printf('%s&nbsp',$c)
                                                            : printf('<a href="?letter=%s">%s</a> ',$c,$c);
                                                        }
                                                        //Other
                                                        print ($letter == '0') ? '#&nbsp' : '<a href="?letter=0">#</a>';
                                                    
                                                        echo "</b><br></div><p>";
                                                        
                                                        //Show all restaurants that start with $letter
                                                        $sql = ($letter == '0')
                                                          ? "SELECT DISTINCT ID, name, address FROM restaurants WHERE UCASE(SUBSTRING(name,1,1)) NOT BETWEEN 'A' AND 'Z'"
                                                          : "SELECT DISTINCT ID, name, address FROM restaurants WHERE UCASE(SUBSTRING(name,1,1)) = '{$letter}'";
                                                        $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']
                                                                );
                                                            
                                                        }
                                                      
                                                      echo $row;
                                                        
                                                        //this outputs all restaurants
                                                        $letter = isset($_GET['letter']) ? $_GET['letter'] : "A";
                                                        
                                                          //Show all restaurants that start with $letter
                                                          /*
                                                          $sql = "SELECT * FROM restaurants";
                                                          $result = mysql_query($sql) or die(mysql_error());
                                                          while($row = mysql_fetch_assoc($result)){
                                                            printf('<div align="left" width="100"><b>%s</b><br>%s</br>%s</br></div>',$row['name'],$row['address'],$result['cviolations']);
                                                          }
                                                          */
                                                      ?>

Hmmm it didn't quite work so I started playing around and have this now...which should work yet doesn't .... it isn't pulling records but it is passing the variable in the url as it should...

 

        $letter = isset($_GET['letter']) ? $_GET['letter'] :"A";


                                                       
                                                        //alphabetical pagination links
                                                        echo '<div align="center"><b>';
                                                        foreach(range('A','Z') as $c){
                                                          ($letter == $c)
                                                            ? printf('%s&nbsp',$c)
                                                            : printf('<a href="?letter=%s">%s</a> ',$c,$c);
                                                        }
                                                        //Other
                                                         foreach(range('0','9') as $n){
                                                          ($letter == $n)
                                                        ? printf('%s&nbsp',$n)
                                                        : printf('<a href="?letter=%s">%s</a> ',$n,$n);
                                                        }
                                                    
                                                        echo "</b><br></div><p>";
                                                        
                                                        //Show all restaurants that start with $letter  not between "A" and "Z"
                                                          //Show all restaurants that start with $letter
                                                        $sql = "SELECT DISTINCT ID, name, address FROM restaurants WHERE name LIKE '{$letter}%'";
                                                        $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']
                                                                );
                                                            
                                                        }
                                                      
                                                      echo $row;
                                                        
                                                        //this outputs all restaurants
                                                        $letter = isset($_GET['letter']) ? $_GET['letter'] : "A";
                                                     

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.