Php 按首字母字母顺序分组
使用以下脚本,可以创建按字母顺序输入,按大写字母排序。 但是,我无法将Php 按首字母字母顺序分组,php,mysqli,Php,Mysqli,使用以下脚本,可以创建按字母顺序输入,按大写字母排序。 但是,我无法将$author放入while语句中,因此它显示为“title” 我希望最后一个html部分包含在while语句中。但不管我怎么做,都不管用 我做错了什么 <?php if ($stmt = $mysqli->prepare("select `link`, `titel`, `author` from `books` where `id`=? order by `titel`asc ")) { $stmt
$author
放入while语句中,因此它显示为“title”
我希望最后一个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];
“最后一个html部分”可以在foreach($valas$reqvals)
中创建(当然要记住$reqvals
是一个列表),因为它可以访问“整行” 作为回答的旁白,您必须
By
您可能想要的是:
<div class="author">By <?php print $author; ?></div>
By
或
By
是的,您需要指示PHP将变量打印到屏幕上
另见:
<div class="<?php print $letter; ?>"></div>
当我这样做时,用print_r($res);,我知道:``数组([D]=>Array([0]=>Array([0]=>Da himmelen kom nærmere[1]=>Brian Patrick McGuire[2]=>239))``所以这些值被取回。但我无法将它们加载到页面上:标题应该是“array”。请帮助。记住,这里的foreach($valas$reqvals)
$reqvals现在是一个数组,所以标题在$reqvals[0]
中。谢谢DinoCoderSaurus。现在我有了它,它工作了。
<div class="author">By <?=$author;?></div>
<div class="<?php print $letter; ?>"></div>