php - 按字母顺序按首字母分组
问题描述
使用以下脚本,在大写字母下按字母顺序输入值。但是,我无法$author
进入 while 语句,因此它显示为“标题”。
我希望将最后一个 html 部分包含在 while 语句中。但无论我尝试什么,它都不起作用我做错了什么?
<?php
if ($stmt = $mysqli->prepare("select `link`, `titel`, `author` from `books` where `id`=? order by `titel`asc ")) {
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->bind_result($link, $titel, $author);
while ($stmt->fetch()) {
$titel = mb_strimwidth("$titel", 0, 30, "...");
$letter = substr($titel, 0, 1);
$res[$letter][] = $titel;
}
foreach ($res as $key => $val) {
?>
<div class="<?php $letter; ?>"></div>
<?php
echo $key; //shows capital letter
foreach ($val as $reqvals) {
echo $reqvals; //shows 'titel'
echo "<br>";
}
}
?>
<a href="#se_annonce<?php echo $link; ?>" class="popup-with-zoom-anim">
<div class="box">
<div class="author">By <?php $author; ?></div>
</div>
</a>
<?php
$stmt->close();
}
?>
解决方案
当while循环执行完毕,没有$link
s或$author
s;他们没有被保存在任何地方。
一种选择是将它们保存在$res
数组中。也许是这样的:
$res[$letter][] = [$titel, $author, $link];
可以在foreach($val as $reqvals)
(当然记住那$reqvals
是一个列表)中创建“最后一个 html 部分”,因为它可以访问“整行”。
推荐阅读
- python - 从变量而不是文件在python opencv中打开下载的图像
- symfony - 如何防止字段带有链接
- amazon-web-services - Elastic Beanstalk 将 http 重定向到 https 用于 iis 站点
- android - 左右滑动的Android可扩展列表视图
- python - 按标题在 Outlook 中查找特定邮件
- google-cloud-platform - 使用 Deployment Manager 进行部署时,上传本地文件以使其在构建步骤中可访问
- python - DJango 中的模型类型
- neo4j - Neo4j 社区/企业/桌面是否可以在没有互联网的情况下工作
- react-native - 如何获取放置在自定义标记或标注视图内的多个可触摸组件的 onPress 事件?
- javascript - 在 react bootstrap 中根据 Form.check 中的选中/取消选中显示/隐藏 Form.control