Jump to content

[SOLVED] Simplest way to create alphabetic anchor links?


dmccabe

Recommended Posts

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++;
	}
}
}

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.

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]

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.