我使用mysqli和php构建了一个手风琴格式的导航页面。你可以看一个例子 [1]:http://www.outerhebridesalgae.uk/marine/marine-taxononic-list.php/“海洋藻类”。 这非常有效,但编码非常重复,因为我为手风琴中的每个标题重复相同的代码。这对于有限数量的标题很好,但我的一些表有30个或更多的标题!有更有效的方法吗?谢谢你的帮助。
代码示例如下:
<?phprequire '../assets/includes/conn.inc.php';
$query1 = mysqli_query($connect, "SELECT * FROM marine WHERE famid='1' ORDER BY marine.species");
$row1 = mysqli_fetch_assoc($query1);
$totalRows_query1 = mysqli_num_rows($query1);
$query2 = mysqli_query($connect, "SELECT * FROM marine WHERE famid='2' ORDER BY marine.species");
$row2= mysqli_fetch_assoc($query2);
$totalRows_query2 = mysqli_num_rows($query2);
$query4 = mysqli_query($connect, "SELECT * FROM marine WHERE famid='4' ORDER BY marine.species");
$row4= mysqli_fetch_assoc($query4);
$totalRows_query4 = mysqli_num_rows($query4);
?>
<!DOCTYPE html>
<head>.......</head>
<body>
<div class="uk-panel uk-panel-box uk-margin">
<h2 class="uk-panel-title">Chlorophyta - Green Seaweeds</h2>
<div id="firstpane" class="msg_list">
<p class="msg_head"> <?php echo $row1['family']; ?><img src="../images/arrow.jpg" alt="arrow open" align="right"></p>
<div class="msg_body">
<table width="100%" >
<?php do { ?>
<tr>
<td id="marine.species"><a href="marine-species.php?id=<?php echo $row1['id']; ?>"><?php echo $row1['species']; ?></a></td>
<?php } while ($row1 = mysqli_fetch_assoc($query1)); ?>
</tr>
</table>
</div>
<p class="msg_head"><?php echo $row2['family']; ?><img src="../images/arrow.jpg" alt="arrow open" align="right"></p>
<div class="msg_body">
<table width="100%" >
<?php do { ?>
<tr>
<td id="marine.species"><a href="marine-species.php?id=<?php echo $row2['id']; ?>"><?php echo $row2['species']; ?></a></td>
<?php } while ($row2 = mysqli_fetch_assoc($query2)); ?>
</tr>
</table>
</div>
<p class="msg_head"><?php echo $row4['family']; ?><img src="../images/arrow.jpg" alt="arrow open" align="right"></p>
<div class="msg_body">
<table width="100%" >
<?php do { ?>
<tr>
<td id="marine.species"><a href="marine-species.php?id=<?php echo $row4['id']; ?>"><?php echo $row4['species']; ?></a></td>
<?php } while ($row4 = mysqli_fetch_assoc($query4)); ?>
</tr>
</table>
</div></body></html>