我想显示"类别"作为链接的书籍和单击链接时(例如:Art),我想提取属于该特定类别的数据(例如:艺术 - >颜色和光由James Gurney,艺术精神由Robert Henry ,David Bayles的Art& Fear等等......)
这是我的数据库结构,我有一个数据库(booksdb),它有两个名为(category)和(listing)的表。
Table : category +--------+----------+ | cat_id | category | +--------+----------+ | 1 | Art | | 2 | Drama | | 3 | Music | | 4 | Fiction | | 5 | Computer | +--------+----------+
Table: listing +----+--------+---------------------------------+-----------------------+ | 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. | +----+--------+---------------------------------+-----------------------+
答案 0 :(得分:-1)
考虑这个例子:
首先,您必须使用PHP提取这些值。提取后,您现在可以在HTML上显示它。您可以使用下拉框(在此示例中为此)选择要查看的类别。
<强>的index.php 强>
<?php
// use $_GET variable for your query
$category = (isset($_GET['category']) && $_GET['category'] != '') ? (int) $_GET['category'] : null;
// connect to mysql
$con = mysqli_connect("localhost","user","password","database");
// build your query
$query_statement = 'SELECT `listing`.`id`, `listing`.`cat_id`, `category`.`category`, `listing`.`title`, `listing`.`author` FROM `listing` LEFT JOIN `category` ON `listing`.`cat_id` = `category`.`cat_id`';
if($category != null) {
$query_statement = $query_statement . " WHERE `category`.`cat_id` = $category";
}
$query = mysqli_query($con, $query_statement);
?>
<!-- HTML -->
<!-- Loop them inside a table -->
<form method="GET" action="index.php">
<select name="category" onchange="this.form.submit()">
<option disabled selected>Select Category</option>
<option value="">All</option>
<option value="1">Art</option>
<option value="2">Drama</option>
<option value="3">Music</option>
<option value="4">Fiction</option>
<option value="5">Computer</option>
</select>
</form>
<table border="1" cellpadding="10">
<thead>
<tr>
<th>Category</th>
<th>Title</th>
<th>Author</th>
</tr>
</thead>
<tbody>
<?php while($result = mysqli_fetch_assoc($query)): ?>
<tr>
<td><?php echo $result['category']; ?></td>
<td><?php echo $result['title']; ?></td>
<td><?php echo $result['author']; ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
编辑:
<!-- LINKS (if you need links you can do something like this) -->
<a href="index.php?category=1">Art</a>
<a href="index.php?category=2">Drama</a>
<a href="index.php?category=3">Music</a>
<a href="index.php?category=4">Fiction</a>
<a href="index.php?category=5">Computer</a>
注意:尝试研究更多这些内容,然后尝试增强其部分内容,例如安全性等。只是将其作为迈向学习更多内容的垫脚石,因为您没有发布了与问题相关的任何代码。