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

时间:2014-08-06 08:28:38

标签: php mysql

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

实施例,

艺术(5)
戏剧(2)
音乐(5)
小说(4)
电脑(5)

而且,这是我的PHP代码;

的index.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>

page.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. |
+----+--------+---------------------------------+-----------------------+

4 个答案:

答案 0 :(得分:0)

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 />';
}

?>

答案 1 :(得分:0)

因为你开始学习PHP和MySQL,所以我不想让事情复杂化。有很多方法可以达到你想要的效果,但我会为你提供一个解决问题的简单方法。

您可以尝试更改代码:

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

致:

while($category = mysql_fetch_assoc($res_query))
{
  $categoryID = $category['cat_id'];

  $queryCount = mysql_query("SELECT     COUNT(id) AS total
                             FROM       books
                             WHERE      cat_id = '$categoryID'");

  $row = mysql_fetch_assoc($queryCount);

  echo '<a href="page.php?cat_id=' . $category['cat_id'] . '">' . $category['category'] . ' (' . $row['total'] . ')</a><br />';

  // Note: Please try to var_dump($row) and look if $row['total'] contains the number of books under on that category.
}

这就是全部,享受学习......

答案 2 :(得分:-1)

尝试使用将对项目进行计数和分组的SQL代码。 我确信这段代码可以正常运行,但它是UN-TESTED。

SELECT COUNT(*) as cnt,category
FROM books
LEFT JOIN bookcatname ON books.cat_id = bookcatname.cat_id
GROUP BY category

然后在你的PHP代码中你会使用这样的东西:

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

修改 您将此代码添加到加载类别的位置。

答案 3 :(得分:-1)

您需要在表“bookcatname”和“books”上使用JOIN opertaion来获取每个类别的总计数。

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

检查演示: http://sqlfiddle.com/#!2/73eb29/13

修改index.php文件的以下部分:

$res_query = mysql_query("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");

while ($category = mysql_fetch_assoc($res_query) ) {

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