请帮帮我。这里的任何人都可以告诉我为什么我的数据库的结果显示这样的链接?
Art
Art
Art
Art
Music
Music
Music
Music
Drama
Drama
Drama
Drama
Medical
Medical
Medical
我希望这些类别显示如下,
Art
Music
Drama
Medical
...这是我index.php
<?php
$dbh=mysql_connect("127.0.0.1","root","root") or die ('Cannot connedt to the Database' .mysql_errno());
mysql_select_db("books");
$res = "SELECT * FROM table1 ORDER BY category ASC";
$res_query = mysql_query($res) or die (mysql_error());
$ra = mysql_fetch_assoc($res_query);
?>
<?php do { ?>
<p><a href="page.php?id=<?php echo $ra['id']; ?>"><?php echo $ra['category']; ?></a></p>
<?php } while ($ra = mysql_fetch_assoc($res_query))?>
...这是我在page.php
中显示结果的代码......
<?php
$dbh=mysql_connect("127.0.0.1","root","root") or die ('Cannot connedt to the Database' .mysql_errno());
mysql_select_db("books");
if(isset($_GET['id'])){
$id = $_GET['id'];
}else {
$id=1;
}
$res = "SELECT * FROM table1 WHERE id=$id";
$res_query = mysql_query($res) or die (mysql_error());
$ra = mysql_fetch_assoc($res_query);
?>
<h1><?php echo $ra['category']; ?></h1>
<p><?php echo $ra['author']; ?></p>
<p><?php echo $ra['title']; ?></p>
下面是我为我的书籍集创建的数据库。
1 |Art | Author1 | title1
2 |Art | Author2 | title2
3 |Art | Author3 | title3
4 |Art | Author4 | title4
5 |Music | Author1 | title1
6 |Music | Author2 | title2
7 |Music | Author3 | title3
8 |Music | Author4 | title4
9 |Drama | Author1 | title1
10 |Drama | Author2 | title2
11 |Drama | Author3 | title3
12 |Drama | Author4 | title4
13 |Medical | Author1 | title1
14 |Medical | Author2 | title2
15 |Medical | Author3 | title3
答案 0 :(得分:0)
试试这个,
更改index.php中的以下行
$res = "SELECT * FROM table1 ORDER BY category ASC";
到
$res = "SELECT * FROM table1 GROUP BY category ORDER BY category ASC";
答案 1 :(得分:0)
好的,我已经创建了一个简单的小例子,可以让你知道如何解决这个问题。它绝不是一个最佳实践的例子(我也没有把很多时间投入其中),但它可以解决问题。
这是专注于功能,而不是性能(仅显示 方式,而不是 方式)。
MySQL中的表格:
图书强>
ID Name Author Description
1 Sample Book 1 Sample Author 1 Small description for book 1
2 Sample Book 2 Sample Author 2 Larger description for book 2 (BECAUSE WE EXTEND THIS BY SOME UNKONWN BLAH)
3 Sample Book 3 Sample Author 3 No description found for 3? Wrong! This is the description!
4 Sample Book 4 Sample Author 4 Description for book 4
5 Sample Book 5 Sample Author 5 Description for book 5
6 Sample Book 6 Sample Author 6 Description for book 6
<强>分类强>
ID name
1 art
2 music
3 drama
4 medical
Category_Book(它们之间的链接表)
ID category_id book_id
1 1 1
2 2 2
3 2 3
4 3 4
5 4 5
6 4 6
然后我有三个.php文件,名为 category-overview.php (显示所有类别), book-overview.php (显示特定类别中的所有书籍) )和 book-view.php (显示有关一本特定书籍的信息)。
这些文件的代码:
<强>类别-overview.php 强>
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
$dbh = mysql_connect("host", "username", "password") or die("Can't connect.");
mysql_select_db("books");
$query = "SELECT * FROM category ORDER BY name ASC";
$execute = mysql_query($query) or die(mysql_error());
$categories = array();
while ($row = mysql_fetch_assoc($execute)) {
$categories[] = $row;
}
foreach ($categories as $category) {
?>
<p><a href="book_overview.php?category=<?php echo $category['name'] ?>"><?php echo ucfirst($category['name']) ?></a> </p>
<?php } ?>
</body>
</html>
图书-overview.php 强>
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
if (isset($_GET['category'])) {
$category = $_GET['category'];
$dbh = mysql_connect("host", "username", "password") or die("Can't connect.");
mysql_select_db("books");
$query = "SELECT * FROM book b WHERE b.id IN (SELECT book_id FROM category_book WHERE category_id = (SELECT id FROM category WHERE name = '$category'));";
$execute = mysql_query($query) or die(mysql_error());
$books = array();
while ($row = mysql_fetch_assoc($execute)) {
$books[] = $row;
}
foreach ($books as $book) {
echo '<b>Book Title:</b> ' . $book['title'] . '<br/>';
echo '<b>Book Written By:</b> ' . $book['author'] . '<br/>';
echo '<a href="book_view.php?id=' . $book['id'] . '">Read more about this book...</a><br/>';
echo '<br/>';
}
}
?>
</body>
</html>
图书-view.php 强>
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
if (isset($_GET['id'])) {
$id = $_GET['id'];
$dbh = mysql_connect("host", "username", "password") or die("Can't connect.");
mysql_select_db("books");
$query = "SELECT * FROM book b WHERE b.id = '$id';";
$execute = mysql_query($query) or die(mysql_error());
$book;
while ($row = mysql_fetch_assoc($execute)) {
$book = $row;
}
if ($book !== null) {
echo '<b>Book Title:</b> ' . $book['title'] . '<br/>';
echo '<b>Book Written By:</b> ' . $book['author'] . '<br/>';
echo '<p>' . $book['description'] . '</p><br/>';
echo '<br/>';
}
}
?>
</body>
</html>
工作原理:
在类别概述中,我所做的只是检索类别并为它们创建超链接。
在书籍概述中,我检查$_GET
变量中的类别。如果有,请检索具有该类别ID的所有书籍(使用链接表)。我通过检索所有书籍信息来获取ID来创建超链接。
在书本视图中,您只需执行相同的操作,但对于一本特定的书(检查$_GET
,如果找到,则检索该特定书籍的数据)。
我希望它能够清楚地说明它应该如何/应该如何工作。如果您有任何疑问,请与我们联系。