<?php // 连接数据库 $conn = new mysqli("localhost", "root", "123456", "wyya"); // 检查连接是否成功 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 查询五个歌单的表 $tables = array(); $result = mysqli_query($conn, "SHOW TABLES LIKE '%_list'"); if ($result->num_rows > 0) { while ($row = mysqli_fetch_array($result)) { $tables[] = $row[0]; } } // 获取选中的歌单表 $tableName = isset($_GET["table"]) ? $_GET["table"] : ""; $data = array(); if (!empty($tableName)) { $result = mysqli_query($conn, "SELECT * FROM $tableName"); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { $data[] = $row; } } } ?> <!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; } .dropdown { position: relative; display: inline-block; } .dropdown-content { display: none; position: absolute; z-index: 1; } .dropdown:hover .dropdown-content { display: block; } </style> </head> <body> <h2>网易云音乐歌单</h2> <div class="dropdown"> <a href="#">分类</a> <div class="dropdown-content"> <?php foreach ($tables as $table) { ?> <a href="?table=<?php echo $table; ?>"><?php echo str_replace("_list", "", $table); ?></a> <?php } ?> </div> </div> <?php if (!empty($tableName)) { ?> <table> <tbody> <tr> <?php $columns = mysqli_query($conn, "SHOW COLUMNS FROM $tableName"); if ($columns->num_rows > 0) { while ($column = mysqli_fetch_array($columns)) { ?> <th><?php echo $column["Field"]; ?></th> <?php } } ?> </tr> <?php foreach ($data as $row) { ?> <tr> <?php foreach ($row as $value) { ?> <td><?php echo $value; ?></td> <?php } ?> </tr> <?php } ?> </tbody> </table> <?php } ?> </body> </html> <?php // 关闭连接 $conn->close(); ?>修改代码,将one修改成华语,two修改成欧美,three修改成日语,four修改成韩语,five修改成粤语
时间: 2023-12-13 15:03:45 浏览: 181
php连接Mysql数据库
可以将以下代码:
<?php foreach ($tables as $table) { ?>
<a href="?table=<?php echo $table; ?>"><?php echo str_replace("_list", "", $table); ?></a>
<?php } ?>
修改为:
<?php
$language = array(
"one" => "华语",
"two" => "欧美",
"three" => "日语",
"four" => "韩语",
"five" => "粤语",
);
foreach ($tables as $table) {
$tableName = str_replace("_list", "", $table);
$languageKey = array_search($tableName, $language);
?>
<a href="?table=<?php echo $table; ?>"><?php echo $language[$languageKey]; ?></a>
<?php } ?>
阅读全文