我正在使用AlphaListNav.js创建一个音乐家的a - z与他们的发行列表。
我的SQL表music
,有字段id
,artists
,year
,title
和genre
我使用以下代码从表中返回artists
的列表,它工作得很好。
<ul id="myList">
<?php
include_once("conn.php");
$sql = "SELECT DISTINCT artists FROM music ORDER BY ASC";
$result = mysqli_query($conn, $sql);
$num_rows = mysqli_num_rows($result);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
?>
<li><?php echo $row["artists"];?>
<?php
}
}
?>
</li>
</ul>
<script src="http://localhost/AlphaListNav.js"></script>
<script> const alphaNav = new AlphaListNav(document.getElementById('myList'));</script>
输出如下:
All │ 0-9 │ A │ B │ C │ D │ E │ F │ G │ H │ I │ J │ K │ L │ M │ N │ etc etc
A
A Day To Remember
A Killers Confession
A La Rock
A.K.A.
ACDC
B
Etc
我想在每行末尾添加一个count
(在li
元素内),用于从music
表中获取每个artists
的记录/行数。
像这样…
A
A Day To Remember (12)
A Killers Confession (4)
A La Rock (8)
如有任何帮助,不胜感激。
GROUP BY是一个键:
SELECT artist, COUNT(title) as music_count FROM music GROUP BY artist