仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 4968|回复: 20
打印 上一主题 下一主题

[学习教程] PHP教程之如何用PHP来给网页做导航栏

[复制链接]
透明 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-16 00:30:10 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
可以在书上很方便地做标记,及时记下自己的心得体会。网页       译者注:本文原名《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");
    $root = dirname($full_path);$page_file = basename($full_path);
    /* PHP函数getenv()用来获得情况变量的值,REQUEST_URI的值是紧跟在主机名后的局部URL,假设URL是http://www.yourmom.com/dinner/tuna_1.html, 那它的值就为/dinner/tuna_1.html. 如今咱们将失掉的那局部URL放在变量$full_path中,再用dirname()函数来从URL中抓取文件目次,用basename()函数获得文件名,用下面的例子来说dirname()前往值:/dinner/;basename()前往:tuna_1.html.接上去的局部绝对有些技能,假设咱们的文件名以story_x的格局定名,个中x代表页码,咱们需求从中将咱们利用的页码抽出来。固然文件名纷歧定只要一名数字的形式或只要一个下划线,它可所以tuna_2.html,一样它还可以叫做tuna_234.html乃至是candy_apple_3.html,而咱们真正想要的就是位于最初一个“_”和“。html”之间的东东。可采取以下办法:*/ $page_num = substr($page_file, strrpos($page_file, "_") + 1, strpos($page_file, ".html") - (strrpos($page_file, "_") + 1));/* substr($string, $start,[$length] )函数给了咱们字符串$string中从$start入手下手、长为$length或到末尾的字串(方括号中的参数是可选项,假如省略$length,substr就会前往给咱们从$start入手下手直到字符串末尾的字符串),正如每个优异的C法式员告知你的那样,代表字符串入手下手的地位入手下手的数字是“0”而不是“1”。
    函数strrpos($string, $what)告知咱们字符串$what在变量$string中最初一次呈现的地位,咱们可以经由过程它找出文件名中最初一个下划线的地位在哪,同理,接着的strpos($string, $what)告知咱们“。html”初次呈现的地位。咱们经由过程应用这三个函数获得在最初一个“_”和“。html”之间的数字(代码中的strpos()+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";
    /*(string)($page_num+1)将数学运算$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>";}
    ?>
  左手拿着MOTOLOLA右手拿着NOKIA,要多潇洒,有多潇洒,哈哈,终于学会了,但是可能这个时候,又会有人不经意的拍拍肩膀对你说:哥们,别高兴的太早,你还是菜鸟,离学会还差着一大截呢!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-9 21:21

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表