dmccabe Posted June 27, 2008 Share Posted June 27, 2008 I have a page that displays all our branch locations on 1 page, but I would like to put a set of links at the top of the page with the letters of the alphabet, which when clicked link you to the first row in the table that begins with that letter? E.g. A - B - C - D - E...Y Aberdeen Alperton Barking Brighton Cardiff ... York Clicking on the Y would take you to the anchor at York. Here is how I am outputting the table at the moment $query = "SELECT * FROM `tbl_branches`"; $result = mysql_query($query); $numrows = mysql_num_rows($result); if (!mysql_query($query)) { die('Error: ' . mysql_error()); } elseif ($numrows < "1") { echo "No branches currently in the branch list."; } else { $a = 0; while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { if ($a % 2 == 0) { $colour = "#f1eff5"; } else { $colour = "#d8d2e3"; } if ($row['live'] == "0") { if ($isadmin == "1") { echo "<tr bgcolor='$colour'> <td ><span class='not_live'>".$row['name']."</span></td> <td class='small'><span class='not_live'>".$row['code']."</span></td> <td><span class='not_live'><a href='http://maps.google.co.uk/maps?f=q&hl=en&geocode=&q=".$row['post_code']."' title='Click to view map of location' target='_blank'>".$row['address'].",".$row['post_code']."</a></span>"; if (strlen($row['new_address']) > 0) { echo "<br /><span class='newaddress'>".$row['new_address'].",".$row['new_post_code']."</span>"; } echo "</td> <td><span class='not_live'>".$row['front_line']."</span></td> <td><span class='not_live'>".$row['back_line']."</span></td> <td><span class='not_live'>".$row['fax']."</span></td> <td><span class='not_live'>".$row['mobile']."</span></td> <td><span class='not_live'>".$row['email']."</span></td> <td><span class='not_live'>".$row['manager_id']."</span></td>"; if ($isadmin == "1") { echo "<td><a href='/xxx/admin/branchedit.php?edit=".$row['id']."' title='Edit details for ".$row['name']."'><img src='/xxx/img/edit_button.gif' title='Edit' alt='Edit' /></a></td>"; } echo "</tr>"; $a++; } } else { echo "<tr bgcolor='$colour'> <td>".$row['name']."</td> <td>".$row['code']."</td> <td><a href='http://maps.google.co.uk/maps?f=q&hl=en&geocode=&q=".$row['post_code']."' title='Click to view map of location' target='_blank'>".$row['address'].",".$row['post_code']."</a>"; if (strlen($row['new_address']) > 0) { echo "<br /><span class='newaddress'>".$row['new_address'].",".$row['new_post_code']."</span>"; } echo "</td> <td>".$row['front_line']."</td> <td>".$row['back_line']."</td> <td>".$row['fax']."</td> <td>".$row['mobile']."</td> <td>".$row['email']."</td> <td>".$row['manager_id']."</td>"; if ($isadmin == "1") { echo "<td><a href='/xxx/admin/branchedit.php?edit=".$row['id']."' title='Edit details for ".$row['name']."'><img src='/xxx/img/edit_button.gif' title='Edit' alt='Edit' /></a></td>"; } echo "</tr>"; $a++; } } } Link to comment https://forums.phpfreaks.com/topic/112150-solved-simplest-way-to-create-alphabetic-anchor-links/ Share on other sites More sharing options...
bluejay002 Posted June 27, 2008 Share Posted June 27, 2008 simplest way to create alphabetic anchor links? do it in SQL (using ORDER BY). It is not only easy but very convenient. Link to comment https://forums.phpfreaks.com/topic/112150-solved-simplest-way-to-create-alphabetic-anchor-links/#findComment-575766 Share on other sites More sharing options...
dmccabe Posted June 27, 2008 Author Share Posted June 27, 2008 I dont really want them re-sorted, I want it so they are all displayed, but clicking "C" will jump you to the section of the list that has the branches that begin with the letter C Link to comment https://forums.phpfreaks.com/topic/112150-solved-simplest-way-to-create-alphabetic-anchor-links/#findComment-575780 Share on other sites More sharing options...
bluejay002 Posted June 27, 2008 Share Posted June 27, 2008 that's the point... you can check at the fetched data if the first character has changed... if yes, create an anchor for that preceding letter. you do not need to be totally aware of what letter it is since they are sorted in that way for you from the query itself. Link to comment https://forums.phpfreaks.com/topic/112150-solved-simplest-way-to-create-alphabetic-anchor-links/#findComment-575787 Share on other sites More sharing options...
dmccabe Posted June 27, 2008 Author Share Posted June 27, 2008 Excellent got it working lovely Link to comment https://forums.phpfreaks.com/topic/112150-solved-simplest-way-to-create-alphabetic-anchor-links/#findComment-575848 Share on other sites More sharing options...
calibius Posted June 27, 2008 Share Posted June 27, 2008 Could you show how you did it please. Link to comment https://forums.phpfreaks.com/topic/112150-solved-simplest-way-to-create-alphabetic-anchor-links/#findComment-576340 Share on other sites More sharing options...
dmccabe Posted June 30, 2008 Author Share Posted June 30, 2008 I at first simply wrote out the letters A to Z and made them in to anchor links: p>Show branches beginning with: <a href="#A">A</a>|<a href="#B">B</a>|<a href="#C">C</a>|<a href="#D">D</a>|<a href="#E">E</a>|<a href="#F">F</a>|</code] Then I used this if statement inside the While loop that was echo'ing out my results: [code=php:0] $newfirstletter = substr($row['name'], 0,1); if ($newfirstletter != $firstletter) { $firstletter = $newfirstletter; echo "<tr> <td class='firstletter' colspan='6'><h3 class='firstletter' id='$firstletter'>$firstletter</h3></td> </tr>"; } [/code] Link to comment https://forums.phpfreaks.com/topic/112150-solved-simplest-way-to-create-alphabetic-anchor-links/#findComment-577908 Share on other sites More sharing options...
bluejay002 Posted June 30, 2008 Share Posted June 30, 2008 ... Link to comment https://forums.phpfreaks.com/topic/112150-solved-simplest-way-to-create-alphabetic-anchor-links/#findComment-577940 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.