doudonglu3764 2011-10-12 04:58
浏览 57
已采纳

上一页/下一页使用PHP链接

I was wondering if someone awesome would be able to help me out? :D

I have 80 php pages (mostly just static html, I am using the php include command for the header and footer) - I want to have next/back buttons on each page, automatically linking to previous/next pages ( page1.php, page2.php, page3, etc).

I would like an easier approach than having to manually link every button on each student profile page to go to the next/previous student pages.

Anyone have any ideas how to do this? :)

*I'm a beginner coder and don't have enough time to learn how to set up a database/cms for this school project.

  • 写回答

3条回答 默认 最新

  • dongye9071 2011-10-12 05:27
    关注

    This is a relatively robust solution (considering the requirements):

    $pinfo = pathinfo($_SERVER["SCRIPT_FILENAME"]);
    $reqpath = dirname($_SERVER["REQUEST_URI"]);
    
    if(preg_match("/(.*?)(\d+)\.php/",  $pinfo["basename"], $matches)) {
        $fnbase = $matches[1];
        $fndir = $pinfo["dirname"];
        $current = intval($matches[2]);
        $next = $current + 1;
        $prior = $current - 1;
        $next_file = $fndir . DIRECTORY_SEPARATOR . $fnbase . $next . ".php";
        $prior_file = $fndir . DIRECTORY_SEPARATOR . $fnbase . $prior . ".php";
    
        if(!file_exists($next_file)) $next_file = false;
        if(!file_exists($prior_file)) $prior_file = false;
    
    
        if($prior_file) {
            $link = $reqpath . DIRECTORY_SEPARATOR . basename($prior_file);
    
            echo "<a href=\"$link\">Prior</a>";
        }
    
        if($prior_file && $next_file) {
            echo " / ";
        }
    
        if($next_file) {
            $link = $reqpath . DIRECTORY_SEPARATOR . basename($next_file);
    
            echo "<a href=\"$link\">Next</a>";
        }
    }
    
    • It checks if the file next/prior actually exists
    • It supports multiple enumerations like {bla1, bla2, bla3} and {foo1, foo2, foo3}
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 经gamit解算的cors站数据再经globk网平差得到的坐标做形变分析
  • ¥15 GD32 SPI通信时我从机原样返回收到的数据怎么弄?
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题