CodeMama Posted July 15, 2009 Share Posted July 15, 2009 I searched and tried a hundred different things ...I can't seem to get my pages to load the restaurants of all one letter...my script is all piecemeal and I have to end of day to get it right...someone please have pity on me!! What I have so far.... if (!isset($_GET['letter'])) {$letter = "A";} else {$letter = $_GET['letter']; $sql="SELECT * FROM restaurants WHERE name LIKE '".$letter."%'"; $result = mysql_query($sql) or die(mysql_error()) ; echo " <div align=left width=100> <b>$result[name]</b><br> $result[address]</br> $result[cviolations]</br> </div> <hr color=#000 width=200></hr> "; } // List not grabbed yet, so run query and store in $_SESSION //alphabetical pagination links if (!isset($_GET['letter'])) {$letter = "A";} else {$letter = $_GET['letter'];} echo '<div align="center"><b>'; for ($i=65; $i<90; $i++) { if ($letter!= chr($i)) {echo '<a href="browse.php?letter='.chr($i).'">';} echo chr($i)." "; if ($letter!= chr($i)) {echo '</a>'; } } echo "</b></div><p>"; $sql="SELECT * FROM restaurants WHERE name LIKE '".$letter."%'"; $query="SELECT * FROM songs WHERE UPPER(SUBSTRING(name,1,1)) NOT BETWEEN 'A' and 'Z'"; $sql = "SELECT DISTINCT name FROM restaurants WHERE name LIKE '$letter.%' GROUP BY name DESC"; $result = mysql_query($sql) or die(mysql_error()) ; $count = mysql_num_rows($result); while($row = mysql_fetch_array($result)) { $name=$row['name']; //prints out restaurant name with link ..... echo "<a href=\"details.php?name=" .$name. "\"> $name</a>\n<br>"; } } trying to have the links a-z on a page that will reload itself when a letter is clicked and display the appropriate records. easy one would think... Link to comment https://forums.phpfreaks.com/topic/166083-solved-help-with-alphabetical-pagination-please/ Share on other sites More sharing options...
rhodesa Posted July 15, 2009 Share Posted July 15, 2009 I saw a few problems...but I still don't completely understand the code. Here it is with the top half rewritten the way I would have done it. Can you load it up with comments on what you are trying to accomplish exactly? <?php $letter = isset($_GET['letter']) ? $_GET['letter'] : "A"; $sql = "SELECT * FROM restaurants WHERE name LIKE '{$letter}%'"; $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><hr color=#000 width=200></hr>',$row['name'],$row['address'],$result['cviolations']); } //alphabetical pagination links echo '<div align="center"><b>'; foreach(range('A','Z') as $c){ ($letter == $c) ? printf('%s ',$c) : printf('<a href="browse.php?letter=%s">%s </a>',$c,$c); } echo "</b></div><p>"; //Not sure what the rest of this code does $sql="SELECT * FROM restaurants WHERE name LIKE '".$letter."%'"; $query="SELECT * FROM songs WHERE UPPER(SUBSTRING(name,1,1)) NOT BETWEEN 'A' and 'Z'"; $sql = "SELECT DISTINCT name FROM restaurants WHERE name LIKE '$letter.%' GROUP BY name DESC"; $result = mysql_query($sql) or die(mysql_error()) ; $count = mysql_num_rows($result); while($row = mysql_fetch_array($result)) { $name=$row['name']; //prints out restaurant name with link ..... echo "<a href=\"details.php?name=" .$name. "\"> $name</a>\n<br>"; } ?> Link to comment https://forums.phpfreaks.com/topic/166083-solved-help-with-alphabetical-pagination-please/#findComment-875882 Share on other sites More sharing options...
CodeMama Posted July 15, 2009 Author Share Posted July 15, 2009 the part I am not able to get working is where if you click on say letter "g" the page should reload and show all the restaurants that begin with G... where does that part go on the page/code? Link to comment https://forums.phpfreaks.com/topic/166083-solved-help-with-alphabetical-pagination-please/#findComment-875899 Share on other sites More sharing options...
rhodesa Posted July 15, 2009 Share Posted July 15, 2009 then just this code should work: <?php $letter = isset($_GET['letter']) ? $_GET['letter'] : "A"; //Show all restaurants that start with $letter $sql = "SELECT * FROM restaurants WHERE UCASE(name) LIKE '{$letter}%'"; $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><hr color=#000 width=200></hr>',$row['name'],$row['address'],$result['cviolations']); } //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></div><p>"; ?> The first part should show all restaurants with the selected letter and the second part should show the letters. Are you getting any errors? Link to comment https://forums.phpfreaks.com/topic/166083-solved-help-with-alphabetical-pagination-please/#findComment-875908 Share on other sites More sharing options...
CodeMama Posted July 15, 2009 Author Share Posted July 15, 2009 all I get is the list of letters a-z with links but when I click on the links the page reloads but does not pull any records from the db.... Link to comment https://forums.phpfreaks.com/topic/166083-solved-help-with-alphabetical-pagination-please/#findComment-875942 Share on other sites More sharing options...
rhodesa Posted July 15, 2009 Share Posted July 15, 2009 what happens if you run this code: <?php $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><hr color=#000 width=200></hr>',$row['name'],$row['address'],$result['cviolations']); } ?> Link to comment https://forums.phpfreaks.com/topic/166083-solved-help-with-alphabetical-pagination-please/#findComment-875952 Share on other sites More sharing options...
CodeMama Posted July 15, 2009 Author Share Posted July 15, 2009 ok that code there lists all the restaurants...now If I can just get it to list only on the letter selected.... like all Arby's for A etc... Link to comment https://forums.phpfreaks.com/topic/166083-solved-help-with-alphabetical-pagination-please/#findComment-875964 Share on other sites More sharing options...
rhodesa Posted July 15, 2009 Share Posted July 15, 2009 I can't see anything wrong with this code: <?php $letter = isset($_GET['letter']) ? $_GET['letter'] : "A"; //Show all restaurants that start with $letter $sql = "SELECT * FROM restaurants WHERE UCASE(name) LIKE '{$letter}%'"; $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><hr color=#000 width=200></hr>',$row['name'],$row['address'],$result['cviolations']); } //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></div><p>"; ?> Link to comment https://forums.phpfreaks.com/topic/166083-solved-help-with-alphabetical-pagination-please/#findComment-875969 Share on other sites More sharing options...
CodeMama Posted July 15, 2009 Author Share Posted July 15, 2009 no for some reason that code only puts the letters a-z up and then nothing happens when they are clicked, it shows NO records.... Link to comment https://forums.phpfreaks.com/topic/166083-solved-help-with-alphabetical-pagination-please/#findComment-876030 Share on other sites More sharing options...
rhodesa Posted July 16, 2009 Share Posted July 16, 2009 The only thing I can think of then is that the first character of the name is a blank character or something...what is the output of: <?php $sql = "SELECT SUBSTR(first_name,1,1) let, count(*) cnt FROM `agents` GROUP BY let"; $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($result)){ print "{$row['let']}: {$row['cnt']}<br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/166083-solved-help-with-alphabetical-pagination-please/#findComment-876410 Share on other sites More sharing options...
CodeMama Posted July 16, 2009 Author Share Posted July 16, 2009 You were exactly right, there is a blank space getting stored infront of the names so I fixed that on the insert side and it works like a charm!! although I need help with the syntax on this part, trying to get the link to work for the view.php page...for each restaurant...keep getting the syntax error "unexpected T_Variable" printf('<a href="view.php?name=$row['name']"><b>%s</b><br>%s</br><br></a>',$row['name'],$row['address']); Link to comment https://forums.phpfreaks.com/topic/166083-solved-help-with-alphabetical-pagination-please/#findComment-876552 Share on other sites More sharing options...
rhodesa Posted July 16, 2009 Share Posted July 16, 2009 you may want to switch back to your print() function instead of printf(). i personally prefer printf() cus i think it looks cleaner. The basic jist of it, is you put a %s wherever you want the variable inserted, and then add the variables as arguments. so it would be like this: printf('<a href="view.php?name=%s"><b>%s</b><br>%s</br><br></a>',$row['name'],$row['name'],$row['address']); Link to comment https://forums.phpfreaks.com/topic/166083-solved-help-with-alphabetical-pagination-please/#findComment-876590 Share on other sites More sharing options...
freshlight Posted September 15, 2009 Share Posted September 15, 2009 Hi Arron I have one query on this code, i am tiring the same code but my lists are bit long and i want to control the alphabetical pagination to further pagination but not getting it pls help on this. hnx Link to comment https://forums.phpfreaks.com/topic/166083-solved-help-with-alphabetical-pagination-please/#findComment-918962 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.