After much tinkering, I have managed to get my code to work where the results are grouped together by the first letter with that first letter being displayed also as shown below:
-A-
Alton Towers
-C-
Cedar Point
Chessington
etc.
I have turned those letter headings into HTML anchors. What I'm trying to do now is run similar code that just displays the first letters in a line at the top of page which are links to each anchor. I can't get the code to work. If I duplicate the code and modify it just to show letters, the original query list doesn't display. Can someone help? Below is the code used to display the query results with the letter headings.
try
{
$sql = 'SELECT park_id, name, town, state, country
FROM tpf_parks ORDER BY name ASC';
$result = $pdo->query($sql);
}
catch (PDOException $e)
{
$error = 'Error fetching parks: ' . $e->getMessage();
//include 'error.html.php';//
exit();
}
$name = '';
foreach($result as $key=>$row){
if(substr($row['name'],0,1)!=$name) echo '<br /><br /><h1>-<a name="'.substr($row['name'],0,1).'">'.substr($row
['name'],0,1).'-</h1></a>';
$name = substr($row['name'],0,1);
echo "<a href='park.php?park_id=".$row['park_id']."'>
<h2>".$row['name']."</h2>
<h3>".$row['town'].", ".$row['state'].", ".$row['country']."</h3></a><hr>";
}