我有这个带有子菜单的菜单的html代码:
<li class="has-submenu">
<a href="#">Services</a>
<div class="mainmenu-submenu">
<div class="mainmenu-submenu-inner">
<div>
<h4>Home</h4>
<ul>
<li><a href="#">Plumbers</a></li>
<li><a href="#">Painters</a></li>
</ul>
<h4>People</h4>
<ul>
<li><a href="#">Babysitter</a></li>
<li><a href="#">Trainer</a></li>
</ul>
<h4>School</h4>
<ul>
<li><a href="#">Teacher</a></li>
</ul>
</div>
<div>
<h4>Machine</h4>
<ul>
<li><a href="#">Mecanic</a></li>
</ul>
</div>
</div>
</div>
</li>
然后我将此表称为&#34; services&#34;:
id | name | service | description
1 Mario Plumber aaa
2 Luigi Plumber aaa
3 Link Painter aaa
4 Zelda Babysitter aaa
5 Sonic Trainer aaa
6 Marth Teacher aaa
7 Ike Trainer aaa
8 Little Mac Mecanic aaa
我想创建一个代码,向我显示仅与该子菜单相关联的结果。例如,如果我在子菜单中按下水管工,我希望它只显示我桌子上的水管工。我使用的PHP代码是:
$query = "SELECT * FROM services WHERE service LIKE service";
$result = mysql_query($query) or die(mysql_error());
$casaArray = array();
但我只能告诉我所有的服务。我是PHP的新手。
感谢您的帮助。
答案 0 :(得分:1)
您只需在菜单中创建一个链接:
<a href="information.php?action=Plumber">Plumbers</a>
然后,为您的信息.php。 (我使用mysql_real_escape_string()函数来阻止SQL injections)
if(!empty($_GET["action"])){
$action = mysql_real_escape_string($_GET["action"]);
$query = "SELECT * FROM services WHERE service='$action'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
echo $row["name"]." - ".$row["description"]."<br>";
} /* END OF WHILE LOOP */
} /* END OF IF NOT EMPTY ACTION */
首先,您可以使用此菜单,其中包含可将其重定向到其他页面的链接。不幸的是,您只给了我们一个包含您想要在下一页显示的信息的表。
首先,让我们创建一个表格,我们将其称为main_services
,它将包含两列:service_id
(自动增量和您的主键)和service
。
service_id | service
1 | Plumber
2 | Painter
3 | Babysitter
4 | Trainer
5 | Teacher
6 | Mechanic
and so on...
然后,让我们改变services
表的表结构。您的服务表格仍有四列:id
,service_id
,name
和description
。我们会将main_services.service_id
编入您services.serviceid
id | service_id | name | description
1 1 Mario aaa
2 1 Luigi aaa
3 2 Link aaa
4 3 Zelda aaa
5 4 Sonic aaa
6 5 Marth aaa
7 4 Ike aaa
8 6 Little Mac aaa
建立完表格后,我们会在您的菜单上创建链接。
<div>
<h4>Options</h4>
<ul>
<?php
$con = new mysqli("YourHost", "Username", "Password", "Database"); /* REPLACE NECESSARY DATA */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if($stmt = $con->prepare("SELECT service_id, service FROM main_service")){
$stmt->execute();
$stmt->bind_result($serviceid,$service);
while($stmt->fetch()){
?>
<li>
<a href="information.php?id=<?php echo $serviceid; ?>"><?php echo $service; ?></a>
</li>
<?php
}
$stmt->close();
}
?>
</ul>
</div>
然后让我们创建您的information.php文件:
<?php
/* LET US FIRST ESTABLISH YOUR CONNECTION TO YOUR DATABASE */
$con = new mysqli("YourHost", "Username", "Password", "Database"); /* REPLACE NECESSARY DATA */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if(!empty($_GET["id"])){
if($stmt = $con->prepare("SELECT name, description FROM services WHERE service_id = ?")){
?>
<table>
<tr>
<th>Name</th>
<th>Description</th>
</tr>
<?php
$stmt->bind_param("i",$_GET["id"]);
$stmt->execute();
$stmt->bind_result($name,$description);
while($stmt->fetch()){
?>
<tr>
<td><?php echo $name; ?></td>
<td><?php echo $description; ?></td>
</tr>
<?php
} /* END OF WHILE LOOP */
?>
</table>
<?php
$stmt->close();
} /* END OF PREPARED STATEMENT */
} /* END OF IF NOT EMPTY ID */
?>
答案 1 :(得分:0)
Fist添加jquery文件(学习链接)
$con = new mysqli("YourHost", "Username", "Password", "Database"); /* REPLACE NECESSARY DATA */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if($stmt = $con->prepare("SELECT service_id, service FROM main_service")){
$stmt->execute();
$stmt->bind_result($serviceid,$service);
while($stmt->fetch()){
?>
<li class="ui-state-disabled">
<a href="information.php?id=<?php echo $serviceid; ?>"><?php echo $service; ?></a>
<---------------here set sub menu sql and take the result if submenu not empty---->
<ul>
<li class="ui-state-disabled">Ada</li>
<li>Saarland</li>
<li>Salzburg an der schönen Donau</li>
</ul>
<---------------here close the submenu loop---->
</li>
<---------------here close the menu loop---->
</ul>
我认为你可以解决这个问题(jQuery菜单)..仔细阅读这个网址并学习 https://jqueryui.com/menu/