简体   繁体   English

来自数据库的php超链接

[英]php hyperlinks from database

I'm very new to php and database programs but trying to learn the language and develop a system for my library. 我对php和数据库程序非常陌生,但尝试学习该语言并为我的图书馆开发系统。 I have a database called booksdb and two tables called "books" which contains titles of books and author and bookcatname which contains categories of books with a unique Category ID. 我有一个名为booksdb的数据库和两个名为“ books”的表,其中包含书名,作者和bookcatname ,其中bookcatname包含具有唯一类别ID的书籍类别。

What I want to do is, display all categories on the page and when one category link is clicked, I want to see the names of books and authors under that particular category displayed on another page, 我想做的是,在页面上显示所有类别,然后单击一个类别链接,我想在另一页上看到该特定类别下的书籍和作者的名字,

Example: 例:

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

Here is my code but it does not work. 这是我的代码,但是不起作用。

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

Your current code to show categories don't work cause your using : 您当前显示分类的代码不起作用,导致您使用:

$ra = mysql_fetch_assoc($res_query);

and after you do it again in a loop : 然后再次循环执行:

while ($ra = mysql_fetch_assoc($res_query)

Do only one while() to get categories : 仅执行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>';
}

Now, you will need another page to get books from this category (or you can do it in same page). 现在,您将需要另一个页面来获取该类别的书籍(或者您可以在同一页面中完成)。

books.php 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>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM