来自数据库的php超链接

时间:2014-08-04 08:34:08

标签: php mysql

我是php和数据库程序的新手,但我试图学习语言并为我的库开发系统。我有一个名为booksdb的数据库和两个名为“books”的表,其中包含书籍和作者的标题,以及bookcatname,其中包含具有唯一类别ID的书籍类别。

我想要做的是,在页面上显示所有类别,当点击一个类别链接时,我希望在另一个页面上显示该特定类别下的书籍和作者的姓名,

示例:

Art;
    Color and Light by James Gurney
    The Art Spirit by Robert Henry
    How Pictures Work by David Bayles
    Imaginative Realism by James Gurney

这是我的代码,但它不起作用。

<?php 

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


  //$res = "SELECT * FROM bookstable GROUP BY category ORDER BY category ASC";
  $res = "SELECT * FROM bookcatname ORDER BY category ASC";


  $res_query = mysql_query($res) or die (mysql_error());
  $ra = mysql_fetch_assoc($res_query);  

?>

<!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 do { ?>
    <p><a href="page.php?cat_id=<?php echo $ra['cat_id']; ?>"><?php echo $ra['category']; ?></a></p>
  <?php } while ($ra = mysql_fetch_assoc($res_query))?>
  </body>
</html>

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

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

1 个答案:

答案 0 :(得分:0)

显示类别的当前代码不起作用导致您的使用:

$ra = mysql_fetch_assoc($res_query);

然后在循环中再次执行此操作:

while ($ra = mysql_fetch_assoc($res_query)

只有一个while()才能获得类别:

$getCategories = 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>';
}

现在,您需要另一个页面来获取此类别的书籍(或者您可以在同一页面中完成)。

<强> books.php

// Same stuff here to connect database

// you check if a category is sent, else you redirect to categories page.
if ( empty($_GET['cat_id']) )
{
  header('Location: index.php');
  exit();
}

// Now you get books from category
// intval() convert to number
$getBooks = mysql_query("SELECT * FROM books WHERE cat_id = '".intval($_GET['cat_id'])."'");

echo '<ul>';

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

echo '</ul>';