php读取redsources_db数据库的red_resources;数据库账号为root,密码为Aa123456。red_resources表头内容为id,category_id,subcategory_id,details,image。 根据数据库动态创建以下内容: 第一个php文件: 需求1:请根据category_id的类别创建一级分类,并且在一级分类下根据subcategory_id的类别创建二级分类; 需求2:每个分类都是唯一的; 需求3:一级分类分类缩进100格,字体大小为40,颜色为蓝色,字体加粗; 需求4:二级分类为单选框,缩进200格,字体大小为20,颜色为蓝色; 需求5:添加按钮,按钮名称为选择种类。当点击按钮,且二级分类的单选框选择项不为空时,跳转到另外一个页面。并且将当前二级分类的subcategory_id值传入到第二个php文件中; 需求6:当跳转时,需关闭当前mysql的连接; 请给出完整代码
时间: 2024-03-23 07:41:44 浏览: 53
请注意,在回答问题之前,我必须提醒您,本人并不是一个真正的PHP开发人员,只能尽力回答您的问题。以下是我能够为您提供的答案:
第一个PHP文件:
```php
<?php
// 连接到数据库
$servername = "localhost";
$username = "root";
$password = "Aa123456";
$dbname = "redsources_db";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检查连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// 获取分类列表
$sql = "SELECT DISTINCT category_id FROM red_resources";
$result = mysqli_query($conn, $sql);
// 输出一级分类
while ($row = mysqli_fetch_assoc($result)) {
echo "<div style='padding-left:100px;font-size:40px;color:blue;font-weight:bold;'>" . $row["category_id"] . "</div>";
echo "<div style='padding-left:200px;'>";
// 获取二级分类列表
$sql2 = "SELECT DISTINCT subcategory_id FROM red_resources WHERE category_id='" . $row["category_id"] . "'";
$result2 = mysqli_query($conn, $sql2);
// 输出二级分类
while ($row2 = mysqli_fetch_assoc($result2)) {
echo "<input type='radio' name='subcategory_id' value='" . $row2["subcategory_id"] . "' style='margin-left:20px;font-size:20px;color:blue;'>" . $row2["subcategory_id"] . "<br>";
}
echo "</div>";
}
// 关闭连接
mysqli_close($conn);
?>
<!-- 添加按钮,点击时跳转到第二个php文件 -->
<button onclick="submitForm()">选择种类</button>
<script>
function submitForm() {
// 检查是否选择了二级分类
var subcategorySelected = false;
var radios = document.getElementsByName('subcategory_id');
for (var i = 0; i < radios.length; i++) {
if (radios[i].checked) {
subcategorySelected = true;
break;
}
}
if (subcategorySelected) {
// 提交表单到第二个php文件
var subcategoryId = document.querySelector('input[name="subcategory_id"]:checked').value;
var form = document.createElement('form');
form.action = 'second_php_file.php';
form.method = 'POST';
var input = document.createElement('input');
input.type = 'hidden';
input.name = 'subcategory_id';
input.value = subcategoryId;
form.appendChild(input);
document.body.appendChild(form);
form.submit();
// 关闭连接
<?php mysqli_close($conn); ?>
} else {
alert('请选择一个二级分类');
}
}
</script>
```
第二个PHP文件:
```php
<?php
// 获取传递过来的二级分类ID
$subcategoryId = $_POST['subcategory_id'];
// 连接到数据库
$servername = "localhost";
$username = "root";
$password = "Aa123456";
$dbname = "redsources_db";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检查连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// 根据二级分类ID获取资源列表
$sql = "SELECT * FROM red_resources WHERE subcategory_id='" . $subcategoryId . "'";
$result = mysqli_query($conn, $sql);
// 输出资源列表
while ($row = mysqli_fetch_assoc($result)) {
echo "<div>";
echo "ID: " . $row["id"] . "<br>";
echo "Category ID: " . $row["category_id"] . "<br>";
echo "Subcategory ID: " . $row["subcategory_id"] . "<br>";
echo "Details: " . $row["details"] . "<br>";
echo "Image: " . $row["image"] . "<br>";
echo "</div>";
}
// 关闭连接
mysqli_close($conn);
?>
```
请注意,以上代码仅作为示例,并未经过充分测试,可能存在错误或漏洞。您应该谨慎使用并自行测试和修改。同时,如果您不熟悉PHP编程,建议您寻求专业的开发人员的帮助。
阅读全文