首页/技术开发/内容

怎么用PHP来给页面做导航栏

技术开发2022-09-15 阅读()
怎样用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 " (北联网教程,专业提供视频软件下载)

第1页  第2页  第3页 

……

相关阅读