gpt4 book ai didi

php - 在 PHP 中显示每个类别的项目数

转载 作者:行者123 更新时间:2023-11-30 23:53:21 26 4
gpt4 key购买 nike

我是 PHP/MYSQL 语言的新手,想知道是否有人可以告诉我如何在下面给出的类别旁边显示项目数。

例子,

艺术 (5)
剧情(2)
音乐 (5)
小说(4)
计算机 (5)

还有,这是我的 php 代码;

索引.php

<?php 

$dbh=mysql_connect("localhost","root","root") or die ('Cannot connedt to the Database' .mysql_errno());
mysql_select_db("booksdb");


?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Select a Company</title>

</head>

<body>


<?php

$res_query = mysql_query("SELECT * FROM bookcatname ORDER BY category ASC");

while ($category = mysql_fetch_assoc($res_query) )
{
echo '<a href="page.php?cat_id='.$category['cat_id'].'">'.$category['category'].'</a><br />';
}

?>
</body>
</html>

页面.php

<?php 

$dbh=mysql_connect("localhost","root","root") or die ('Cannot connect to the Database' .mysql_errno());
mysql_select_db("booksdb");

if ( empty($_GET['cat_id']) )
{
header('Location: index.php');
exit();
}



$getCats = mysql_query("SELECT * FROM books WHERE cat_id = '".intval($_GET['cat_id'])."'");

echo '<ul>';

while ( $book = mysql_fetch_assoc($getCats) )
{
echo '<li>'.$book['title'].'<br />'.$book['author'].'<br />'.'</li><br />';
}

echo '</ul>';
?>

这是表格;

table name - bookcatname

+----+--------+----------+
| id | cat_id | category |
+----+--------+----------+
| 1 | 1 | Art |
| 2 | 2 | Drama |
| 3 | 3 | Music |
| 4 | 4 | Fiction |
| 5 | 5 | Computer |
+----+--------+----------+


table name - books

+----+--------+---------------------------------+-----------------------+
| id | cat_id | title | author |
+----+--------+---------------------------------+-----------------------+
| 1 | 1 | Color and Light | James Gurney |
| 2 | 1 | The Art Spirit | Robert Henry |
| 3 | 1 | Art & Fear | David Bayles |
| 4 | 1 | How Pictures Work | Molly Bang |
| 5 | 1 | Imaginative Realism | James Gurney |
| 6 | 2 | A Walk To Remember | Nicholas Sparks |
| 7 | 2 | An Old Fashioned Girl | Louisa May Alcott |
| 8 | 3 | The Rest Is Noise | Alex Ross |
| 9 | 3 | It Still Moves | Amanda Petrusich |
| 10 | 3 | Chronicles | Bob Dylan |
| 11 | 3 | Dream Boogie | Peter Guralnick |
| 12 | 3 | Escaping The Delta | Robert Johnson |
| 13 | 4 | Atlas Shrugged | Ayn Rand |
| 14 | 4 | Anthem | Ayn Rand |
| 15 | 4 | Sons and Lovers | D.H. Lawrence |
| 16 | 4 | Henderson the Rain King | Saul Bellow |
| 17 | 5 | The Art of Computer Programming | Donald Knuth |
| 18 | 5 | The Art of Unix Programming | Eric Raymond |
| 19 | 5 | Free Software, Free Society | Richard M. Stallman |
| 20 | 5 | Database System Concepts | Abraham Silberschatz |
| 21 | 5 | 3ds Max 2008 in Simple Steps | Kognet Solutions Inc. |
+----+--------+---------------------------------+-----------------------+

最佳答案

SELECT A.cat_id as cat_id, count(A.cat_id) as cnt, B.category as cat_name
FROM books A, bookcatname B
WHERE A.cat_id=B.cat_id
GROUP BY A.cat_id

更新 2:

<?php 

$sql = "SELECT A.cat_id as cat_id, count(A.cat_id) as cnt, B.category as cat_name
FROM books A, bookcatname B
WHERE A.cat_id=B.cat_id
GROUP BY A.cat_id";
$res_query = mysql_query($sql);

while ($category = mysql_fetch_assoc($res_query) )
{
echo '<a href="page.php?cat_id='.$category['cat_id'].'">'.$category['cat_name'].'</a><br />';
}

?>

关于php - 在 PHP 中显示每个类别的项目数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25155580/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com