gpt4 book ai didi

php - 如何在 php 中突出显示选定的选项卡?

转载 作者:搜寻专家 更新时间:2023-10-30 23:09:45 25 4
gpt4 key购买 nike

<?php
include("admin/db.php");

echo '<div class="rmm"><ul id="nav">

<li class="home"><a href="index.php"><span>Home</span></a></li>

<li class="about"><a href="aboutus.php"><span>About Us</span></a></li>';
$sql="SELECT * FROM category WHERE 1";
$result=mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo '<li class="'.$row['cname'].'">'."<a href='category.php?id=".$row["cid"]."'>".$row['cname']."</a></li>" ;
}
echo '<li class="level2 nav-2-3-2"><a href="contactus.php"><span>Contact Us</span></a></li></ul></div>';
?>

我有这样的菜单页面。如果它获得选择选项,我想突出显示选项卡。假设每个菜单都有不同的页面。我可以为每个页面使用 id。但在这里我不知道。任何人都可以用代码解释答案吗?

最佳答案

这是一种方式。您获得 ($_GET) 类别查询字符串的 ID(如果有)并查看它是否与我们当前在列表中打印的 ID 相同。如果是,则给它一些不同的效果,通过将链接的 ID 设置为“当前”,将链接突出显示为“当前打开”,这是样式表代码中定义的样式,现在您有一个突出显示的链接。

这种技术的优点是您无需担心 JavaScript 兼容性,因为突出显示会打印到 HTML 文件本身,这样它就被视为 native 代码并且可以完美运行。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Untitled Document</title>
<style>
body {
font-family: Arial, Helvetica Neue, Helvetica, sans-serif;
font-size: 13px;
color: #151515;
}

ul {
list-style: none;
}

li {
margin: 15px;
}

a {
padding: 5px 10px;
color: #151515;
text-decoration: none;
}

a:hover {
color: #FEFEFE;
background-color: #151515;
}

a#current {
color: #BC1515;
background-color: #EFEFEF;
}
</style>
</head>
<body>
<ul>
<?php
$pages = array("How to do this", "How to do that", "How did you do this", "How did you do that", "Why did you do that", "Why did you do this");
$currentID = (isset($_GET['id']) ? $_GET['id'] : 0);

foreach($pages as $key => $value) {
$key = $key + 1;
print('<li><a href="index.php?page=category&id=' . $key . '"' . ($currentID == $key ? ' id="current"' : '') . '>' . $value . '</a></li>');
}
?>
</ul>
</body>
</html>

结果

Highlighted link has a slightly grayer background and red text

如您所见,突出显示的链接具有略带灰色的背景和红色文本。如果您切换到另一个类别,它会在您位于该页面时突出显示该类别。

希望这对您有所帮助。

编辑:这是相同代码的 MySQL 版本,应该以相同的方式运行:-)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Untitled Document</title>
<style>
body {
font-family: Arial, Helvetica Neue, Helvetica, sans-serif;
font-size: 13px;
color: #151515;
}

ul {
list-style: none;
}

li {
margin: 15px;
}

a {
padding: 5px 10px;
color: #151515;
text-decoration: none;
}

a:hover {
color: #FEFEFE;
background-color: #151515;
}

a#current {
color: #BC1515;
background-color: #EFEFEF;
}
</style>
</head>
<body>
<ul>
<?php
include("admin/db.php");

echo('<div class="rmm"><ul id="nav">
<li class="home"><a href="index.php"><span>Home</span></a></li>
<li class="about"><a href="aboutus.php"><span>About Us</span></a></li>');

$currentID = (isset($_GET['id']) ? $_GET['id'] : 0);
$sql = "SELECT * FROM `category`";
$result = mysql_query($sql);

while($row = mysql_fetch_array($result)) {
echo('<li><a href="category.php?id=' . $row['cid'] . '"' . ($currentID == $row['cid'] ? ' id="current"' : '') . '>' . $row['cname'] . '</a></li>');
}

echo('<li class="level2 nav-2-3-2"><a href="contactus.php"><span>Contact Us</span></a></li></ul></div>');
?>
</ul>
</body>
</html>

关于php - 如何在 php 中突出显示选定的选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21695893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com