CodeMama Posted July 24, 2009 Share Posted July 24, 2009 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 ',$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']); } */ ?> Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 24, 2009 Share Posted July 24, 2009 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 ',$c) : printf('<a href="?letter=%s">%s</a> ',$c,$c); } //Other print ($letter == '0') ? '# ' : '<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']); } */ ?> Quote Link to comment Share on other sites More sharing options...
CodeMama Posted July 24, 2009 Author Share Posted July 24, 2009 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 ',$c) : printf('<a href="?letter=%s">%s</a> ',$c,$c); } //Other foreach(range('0','9') as $n){ ($letter == $n) ? printf('%s ',$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"; Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 24, 2009 Share Posted July 24, 2009 and there are records that start with numbers in the DB, and the number is definitely the first character in 'name'? no whitespace? Quote Link to comment 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.