基于用户HTML表格选择,用PHP从CSV展示对应行数据的实现问题
实现步骤
1. 修改原页面(展示书籍列表)
将第一列的每个书籍名称改为可点击链接,把选中的书籍名称作为参数传递到新页面。修改原PHP代码中数据行的输出部分:
原数据行输出代码:
echo '<td>'.$value.'</td>';
替换为:
echo '<td><a href="view_book.php?book=' . urlencode($value) . '">' . $value . '</a></td>';
完整修改后的原页面代码:
<!DOCTYPE html> <html> <head> <link rel="stylesheet" href="./css/styles.css"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Book Submission</title> </head> <body style="font-family:Verdana;color:#aaaaaa;"> <div style="background-color:#e5e5e5;padding:15px;text-align:center;"> <h1>View Publications</h1> </div> <div style="overflow:auto"> <div class="menu"> <a href="./index.php">HOME</a> <a href="./CV.php">CV</a> <a href="./forecast.php">Weather</a> <?php session_start(); if(!isset($_SESSION['loggedin']))echo "<a href='./session.php'>Log in</a>"; else {echo "<a href='./session.php'>Admin Panel</a>";} ?> </div> <div class="main"> <h2>All Publications on File</h2> <?php $row = 1; if (($handle = fopen("publications.csv", "r")) !== FALSE) { echo '<table border="1">'; while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $num = count($data); if ($row == 1) { echo '<thead><tr>'; }else{ echo '<tr>'; } $c=0; if(empty($data[$c])) { $value = " "; }else{ $value = $data[$c]; } if ($row == 1) { echo '<th>'.$value.'</th>'; }else{ // 修改为可点击链接 echo '<td><a href="view_book.php?book=' . urlencode($value) . '">' . $value . '</a></td>'; } if ($row == 1) { echo '</tr></thead><tbody>'; }else{ echo '</tr>'; } $row++; } echo '</tbody></table>'; fclose($handle); } ?> </div> <div class="right"> <h2>Users </h2> <?php if(isset($_SESSION['loggedin']))echo "<p> You Are Logged in as an admin</p>"; else {echo "<p> No Admins Logged in!!</p>";} ?> </div> </div> <div style="background-color:#e5e5e5;text-align:center;padding:10px;margin-top:7px;">#STUDENT NUMBER</div> </body> </html>
2. 创建新页面(view_book.php)
该页面接收传递的书籍名称,读取CSV文件并过滤出对应行,展示所有列内容:
<!DOCTYPE html> <html> <head> <link rel="stylesheet" href="./css/styles.css"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Book Details</title> </head> <body style="font-family:Verdana;color:#aaaaaa;"> <div style="background-color:#e5e5e5;padding:15px;text-align:center;"> <h1>Book Details</h1> </div> <div style="overflow:auto"> <div class="menu"> <a href="./index.php">HOME</a> <a href="./CV.php">CV</a> <a href="./forecast.php">Weather</a> <?php session_start(); if(!isset($_SESSION['loggedin']))echo "<a href='./session.php'>Log in</a>"; else {echo "<a href='./session.php'>Admin Panel</a>";} ?> </div> <div class="main"> <?php // 接收传递的书籍名称 if (!isset($_GET['book'])) { echo "<h2>未选择书籍</h2>"; exit; } $selected_book = urldecode($_GET['book']); $row = 1; $headers = []; $found_row = null; if (($handle = fopen("publications.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { if ($row == 1) { // 保存表头 $headers = $data; } else { // 判断第一列是否匹配选中的书籍 if ($data[0] == $selected_book) { $found_row = $data; break; } } $row++; } fclose($handle); } if ($found_row) { echo "<h2>".$selected_book." 的详细信息</h2>"; echo '<table border="1">'; echo '<thead><tr>'; // 输出表头 foreach ($headers as $header) { echo '<th>'.(empty($header) ? " " : $header).'</th>'; } echo '</tr></thead><tbody><tr>'; // 输出对应行的所有列 foreach ($found_row as $value) { echo '<td>'.(empty($value) ? " " : $value).'</td>'; } echo '</tr></tbody></table>'; } else { echo "<h2>未找到对应书籍的信息</h2>"; } ?> </div> <div class="right"> <h2>Users </h2> <?php if(isset($_SESSION['loggedin']))echo "<p> You Are Logged in as an admin</p>"; else {echo "<p> No Admins Logged in!!</p>";} ?> </div> </div> <div style="background-color:#e5e5e5;text-align:center;padding:10px;margin-top:7px;">#STUDENT NUMBER</div> </body> </html>
关键说明
- 使用
urlencode()和urldecode()处理参数,避免书籍名称包含特殊字符时传递出错 - 新页面先读取CSV表头,再遍历查找匹配行,确保展示列与表头对应
- 增加参数校验和未找到行的提示,提升用户体验
内容的提问来源于stack exchange,提问作者Xray25
相关产品推荐
相关产品推荐

