<?php // 连接数据库 $conn = new mysqli("localhost", "root", "123456", "wyya"); // 检查连接是否成功 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 查询数据 $tables = array("one_list", "two_list", "three_list", "four_list", "five_list"); $data = array(); foreach ($tables as $table) { $sql = "SELECT * FROM " . $table; $result = mysqli_query($conn, $sql); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { $row['语种'] = substr($table, 0, -5); // 获取语种名称 $data[] = $row; } } } // 渲染 HTML 模板 ?> <!DOCTYPE html> <html> <head> <title>网易云音乐歌单</title> <style> table { border-collapse: collapse; width: 100%; } th, td { text-align: left; padding: 8px; } tr:nth-child(even) { background-color: #f2f2f2; } th { background-color: #4CAF50; color: white; } .language-button { background-color: #f2f2f2; padding: 8px 16px; border: none; cursor: pointer; display: inline-block; margin-right: 10px; } .chinese { color: #e60000; } .english { color: #0066cc; } .japanese { color: #ff9900; } .korean { color: #3385ff; } .cantonese { color: #009933; } </style> </head> <body> <div> <h2>网易云音乐歌单</h2> <div> <form method="get" action=""> <input type="hidden" name="table" value="<?php echo $tables[0]; ?>"> <button type="submit" class="language-button chinese">华语</button> </form> <form method="get" action=""> <input type="hidden" name="table" value="<?php echo $tables[1]; ?>"> <button type="submit" class="language-button english">英语</button> </form> <form method="get" action=""> <input type="hidden" name="table" value="<?php echo $tables[2]; ?>"> <button type="submit" class="language-button japanese">日语</button> </form> <form method="get" action=""> <input type="hidden" name="table" value="<?php echo $tables[3]; ?>"> <button type="submit" class="language-button korean">韩语</button> </form> <form method="get" action=""> <input type="hidden" name="table" value="<?php echo $tables[4]; ?>"> <button type="submit" class="language-button cantonese">粤语</button> </form> </div> </div> <?php if (!empty($data)) { ?> <table> <tbody> <tr> <th>歌单名</th> <th>歌单地址</th> <th>歌曲量</th> <th>播放量</th> <th>收藏量</th> <th>评论量</th> <th>分享量</th> <th>创建者</th> <th>创建时间</th> </tr> <?php foreach ($data as $row) { ?> <tr> <td><?php echo $row['歌单名']; ?></td> <td><a href="<?php echo $row['歌单地址']; ?>" target="_blank"><?php echo $row['歌单地址']; ?></a></td> <td><?php echo $row['歌曲量']; ?></td> <td><?php echo $row['播放量']; ?></td> <td><?php echo $row['收藏量']; ?></td> <td><?php echo $row['评论量']; ?></td> <td><?php echo $row['分享量']; ?></td> <td><?php echo $row['创建者']; ?></td> <td><?php echo $row['创建时间']; ?></td> </tr> <?php } ?> </tbody> </table> <?php } else { ?> <p>暂无数据</p> <?php } ?> </body> </html> <?php // 关闭连接 $conn->close()0>修改代码,通过点击按钮展示出对应的单个歌单
时间: 2023-08-14 22:26:23 浏览: 218
php连接Mysql数据库
好的,您可以将代码修改为以下内容:
```php
<?php
// 连接数据库
$conn = new mysqli("localhost", "root", "123456", "wyya");
// 检查连接是否成功
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
$tables = array("one_list", "two_list", "three_list", "four_list", "five_list");
$current_table = isset($_GET['table']) ? $_GET['table'] : $tables[0]; // 获取当前语种
// 查询数据
$sql = "SELECT * FROM " . $current_table;
$result = mysqli_query($conn, $sql);
$data = array();
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$row['语种'] = substr($current_table, 0, -5); // 获取语种名称
$data[] = $row;
}
}
// 渲染 HTML 模板
?>
<!DOCTYPE html>
<html>
<head>
<title>网易云音乐歌单</title>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #f2f2f2;
}
th {
background-color: #4CAF50;
color: white;
}
.language-button {
background-color: #f2f2f2;
padding: 8px 16px;
border: none;
cursor: pointer;
display: inline-block;
margin-right: 10px;
}
.chinese {
color: #e60000;
}
.english {
color: #0066cc;
}
.japanese {
color: #ff9900;
}
.korean {
color: #3385ff;
}
.cantonese {
color: #009933;
}
</style>
</head>
<body>
<div>
<h2>网易云音乐歌单</h2>
<div>
<?php foreach ($tables as $table) { ?>
<form method="get" action="">
<input type="hidden" name="table" value="<?php echo $table; ?>">
<button type="submit" class="language-button <?php echo substr($table, 0, -5); ?>" <?php echo $table == $current_table ? 'disabled' : ''; ?>><?php echo substr($table, 0, -5); ?></button>
</form>
<?php } ?>
</div>
</div>
<?php if (!empty($data)) { ?>
<table>
<tbody>
<tr>
<th>歌单名</th>
<th>歌单地址</th>
<th>歌曲量</th>
<th>播放量</th>
<th>收藏量</th>
<th>评论量</th>
<th>分享量</th>
<th>创建者</th>
<th>创建时间</th>
</tr>
<?php foreach ($data as $row) { ?>
<tr>
<td><?php echo $row['歌单名']; ?></td>
<td><a href="<?php echo $row['歌单地址']; ?>" target="_blank"><?php echo $row['歌单地址']; ?></a></td>
<td><?php echo $row['歌曲量']; ?></td>
<td><?php echo $row['播放量']; ?></td>
<td><?php echo $row['收藏量']; ?></td>
<td><?php echo $row['评论量']; ?></td>
<td><?php echo $row['分享量']; ?></td>
<td><?php echo $row['创建者']; ?></td>
<td><?php echo $row['创建时间']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<?php } else { ?>
<p>暂无数据</p>
<?php } ?>
</body>
</html>
<?php
// 关闭连接
$conn->close();
?>
```
修改后,您可以点击对应语种的按钮,展示出该语种对应的单个歌单。
阅读全文