php读取redsources_db数据库的red_resources;数据库账号为root,密码为Aa123456。red_resources表头内容为id,category_id,subcategory_id,details,image。 根据数据库动态创建以下内容: 第一个php文件: 需求1:接受外部传入的$subcategoryId = $_GET['subcategory']; 需求2:根据传入的subcategory_id,在数据库中查询该subcategory_id的相同种类的总数量; 需求3:默认显示subcategory_id相同种类的第一条信息; 需求4:添加导览按钮,点击“<<”显示前一个信息,点击“>>”显示后一个信息。 请给出完整代码
时间: 2024-03-23 11:42:51 浏览: 39
以下是符合您要求的完整代码:
1. index.php
```php
<?php
// 连接数据库
$db = mysqli_connect("localhost", "root", "Aa123456", "redsources_db");
// 获取传入的 subcategory_id
$subcategoryId = $_GET['subcategory'];
// 查询相同种类的总数量
$query = "SELECT COUNT(*) AS total FROM red_resources WHERE subcategory_id = '$subcategoryId'";
$result = mysqli_query($db, $query);
$data = mysqli_fetch_assoc($result);
$total = $data['total'];
// 获取要显示的记录的 id
if(isset($_GET['id'])) {
$id = $_GET['id'];
} else {
$id = 1; // 默认显示第一条记录
}
// 查询要显示的记录的详细信息
$query = "SELECT * FROM red_resources WHERE subcategory_id = '$subcategoryId' AND id = '$id'";
$result = mysqli_query($db, $query);
$data = mysqli_fetch_assoc($result);
$category_id = $data['category_id'];
$subcategory_id = $data['subcategory_id'];
$details = $data['details'];
$image = $data['image'];
// 显示记录的详细信息
echo "<h1>Category: $category_id, Subcategory: $subcategory_id</h1>";
echo "<p>$details</p>";
echo "<img src='$image' alt='image'>";
// 显示导览按钮
if($id > 1) {
$prevId = $id - 1;
echo "<a href='index.php?subcategory=$subcategory_id&id=$prevId'><< Prev</a> ";
}
if($id < $total) {
$nextId = $id + 1;
echo "<a href='index.php?subcategory=$subcategory_id&id=$nextId'>Next >></a>";
}
```
2. 数据库结构
表名: red_resources
字段: id, category_id, subcategory_id, details, image
3. 数据库示例数据
id | category_id | subcategory_id | details | image
---|-------------|----------------|---------|------
1 | 1 | 1 | details1| image1.jpg
2 | 1 | 1 | details2| image2.jpg
3 | 1 | 1 | details3| image3.jpg
4 | 1 | 2 | details4| image4.jpg
5 | 1 | 2 | details5| image5.jpg
6 | 2 | 3 | details6| image6.jpg
注:以上示例数据仅供参考,实际应根据需求修改。
阅读全文