首页 > PHP

怎样用PHP来给网页做导航栏

时间:2009-11-17 23:30:57  作者:chinaitlab  我要投稿
Linux初探欢迎您的投稿,投放方法请点击这里查看,我们会定期赠送精美小礼品给优秀的投稿作者。海纳百川 取则行远!LinuxGoo欢迎您的到来。
怎样用PHP来给网页做导航栏
原作:Brad Bulger
译文:李平
译者注:本文原名《Site Navigation with PHP》,原文详述了如何用PHP编程来做出效果理想的网页导航条,本......
怎样用PHP来给网页做导航栏
原作:Brad Bulger
译文:李平
译者注:本文原名《Site Navigation with PHP》,原文详述了如何用PHP编程来做出效果理想的网页导航条,本文只选译了其中的部分文章,所选取的部分是文章精髓之所在,只要大家能弄懂这部分内容就可以用同样的原理、思想做出我们需要的效果来,希望给读者能起到抛砖引玉的作用。本文只需要读者具备PHP、HTML的初步知识就可以基本读懂了。

译 文:如大家所知PHP对于用数据库驱动的网站(making database-driven sites)来讲可谓功能强大,可是我们是否可以用它来做点其他事情呢?PHP给了我们所有我们期望的工具:for与while的循环结构、数学运算等等,还可以通过两种方式来引用文件:直接引用或向服务器提出申请。其实何止这些,让我们来看一个如何用它来做导航条的例子:
完整的原代码:
<!-- This '<?' is how you indicate the start of a block of PHP code, -->
<?php
# and this '#' makes this a PHP comment.

$full_path = getenv("REQUEST_URI");

$root = dirname($full_path);
$page_file = basename($full_path);
$page_num = substr($page_file
, strrpos($page_file, "_") 1
, strpos($page_file, ".html") - (strrpos($page_file, "_") 1)
);

$partial_path = substr($page_file, 0, strrpos($page_file, "_"));

$prev_page_file = $partial_path . "_" . (string)($page_num-1) . ".html";
$next_page_file = $partial_path . "_" . (string)($page_num 1) . ".html";

$prev_exists = file_exists($prev_page_file);
$next_exists = file_exists($next_page_file);

if ($prev_exists)
{
print "<a href="$root/$prev_page_file">previous</a>";
if ($next_exists)
{
print " | ";
}
}
if ($next_exists)
{
print "<a href="$root/$next_page_file">next</a>";
}

?>//原程序完。

代码分析:
OK! 前面做了足够的铺垫工作,现在让我们来看看如何来用PHP来完成这项工作:

<!-- This '<?' is how you indicate the start of a block of PHP code, -->
<?php
# and this '#' makes this a PHP comment.

$full_path = getenv("REQUEST_URI");
如果您需转载 怎样用PHP来给网页做导航栏,请注明来自LinuxGoo.com,其版权归原作者所有。请广大网友留言时遵纪守法,使用文明用语。如果您在应用中有什么问题,请在下面留言,我们会尽快解答。
来顶一下
近回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
相关文章
栏目热门