使用php创建一个注册表单
Highlighting the current page that is being viewed in a navigation bar is a popular design choice for many websites. The CSS is easy: make a class for the navigational element that represents the current page in a linked stylesheet:
在许多网站中,突出显示正在导航栏中查看的当前页面是一种流行的设计选择。 CSS很简单:在链接的样式表中为表示当前页面的导航元素创建一个类:
a.current {
background: black;
color: white;
}
Then apply the class to the appropriate element for the current page. Using the previous exercise as an example:
然后将类应用于当前页面的适当元素。 以上一个练习为例:
<nav role="navigation">
<a href=index.php>Home</a>
<a href="about.php">About Us</a>
<a href="popular-routes-to-india.php" class="current?">Popular Routes To India</a>
<a href="prices.php">Prices</a>
</nav>
At first glance, this would imply that taking the design option of highlighting the current page would stymie our goal of having a single include
, or at least set it back: we’d now have to make the navigation code unique to every page in order to insert the .current
class into the appropriate link for each page. That means more work for us, and greater chances of mistakes and inconsistencies in content.
乍一看,这意味着采用突出显示当前页面的设计选项将妨碍我们实现一个include
或至少将其设置回去的目标:我们现在必须使导航代码对每个页面都唯一将.current
类插入每个页面的相应链接。 这意味着我们需要做更多的工作,内容上出现错误和不一致的机会也更大。
However, with PHP, we do know the name of the page we are currently on: in the last exercise, we have that information contained in the PHP constant title
. In that case, we can keep our single include()
, and make the application of the .current
class dynamic in pagestart.php. For each link, we simply have to see if we are on the appropriate page that matches the link, and apply the .current
class if we are. (For clarity, only two of the navigational list items are shown below, with sections of code placed on separate lines to enhance readability).
但是,使用PHP ,我们确实知道当前页面的名称:在上一练习中,我们在PHP常量title
包含了该信息。 在这种情况下,我们可以保留单个include()
,并在.current
类的应用程序动态化 。 对于每个链接,我们只需要查看我们是否在与该链接相匹配的适当页面上即可,如果可以,则应用.current
类。 (为清楚起见,下面仅显示了两个导航列表项,并且将代码段放在单独的行上以提高可读性)。
<a href="about-us.php" class=<?php if (title=="About Us") echo "current"; ?>">About Us</a>
<a href="popular-routes-to-india.php" class="<?php if (title=="Popular Routes To India") { echo "current"; } ?>">
Popular Routes To India</a>
If we are not on a page that matches title
, the link class
will be set to ""
, which is valid; if we are on a page that matches, class
will be set to current
and the appropriate CSS will be applied to the link.
如果我们不在与title
匹配的页面上,则链接class
将设置为""
,这是有效的; 如果我们在匹配的页面上,则将class
设置为current
,并将适当CSS应用于链接。
Once you know the PHP to produce it, making navigation that is “self-aware” is easy. Adding new links to the navigation bar is as simple as working out the appropriate title
value for the associated filename and placing the if
statement for the link.
一旦知道了要生成PHP,就可以轻松进行“自我感知”的导航。 向导航栏添加新链接非常简单,只需为关联的文件名确定适当的title
值,然后为链接放置if
语句即可。
翻译自: https://thenewcode.com/212/Creating-Self-Aware-Navigation-With-PHP-Part-II
使用php创建一个注册表单