这是我的查询:
try
{
$sql = 'SELECT park_id, name, town, state, country
FROM tpf_parks wHERE ORDER BY name ASC';
$result = $pdo->query($sql);
}
catch (PDOException $e)
{
$error = 'Error fetching parks: ' . $e->getMessage();
//include 'error.html.php';//
exit();
}
$output = 'Parks Loaded';
//include 'output.html.php';//
foreach ($result as $row)
{
$parklist[] = array(
'park_id' => $row['park_id'],
'name' => $row['name'],
'town' => $row['town'],
'state' => $row['state'],
'country' => $row['country']
);
}
include 'parks.html.php';
这是parks.html.php
<?php foreach ($parklist as $park):?>
<a href="park.php?park_id=<?php echo $park['park_id'];?>">
<h2><?php echo $park['name'];?></h2>
<h3><?php echo $park['town'],',',$park['state'],',',$park['country'];?></h3>
</a>
<hr>
<?php endforeach; ?>
我正在寻找可以更改 foreach 循环的某种方式,以便我可以将结果分成按第一个字母排序的组。这样我就可以为每个 Letter 放置一个 html 锚点,使用户可以通过单击页面顶部的链接(ABCDE 等)更轻松地找到特定记录。我不知道如何实现这一目标。