简体   繁体   English

如何在php中包含页面

[英]how to include a page in php

I usually use this code below to include the page that I need into the body of my website to include the page when clicking on a link. 我通常使用下面的代码将我需要的页面包含在网站的主体中,以在单击链接时包含该页面。

<?php
        switch($_GET['page']){

            case '1':
            if(file_exists('main.php'))
            {
        include_once('main.php');
            break;
            }


            default:
        include_once('main.php');
            break;
            }
?>

but then I have to change this everytime i add a menu item by adding a case '2' ... etc and now my question can this be written shorter/dynamically so that i just can add a link without having to change the piece of code everywhere? 但是然后我必须每次通过添加大小写'2'...等添加菜单项时更改此设置,现在我的问题可以更短/更动态地编写,以便我可以添加链接而不必更改代码无处不在?

ps: i did made it a little bit shorter.. but its still not good enough i think.. ps:我确实把它缩短了一点..但是我认为它还不够好..

i also want to add this: i get my links from a ini file. 我也想添加此内容:我从ini文件获取链接。 i place it in there like this: 我把它放在这样的地方:

[navigation] main.php = "Home" [导航] main.php =“主页”

if (!isset($_GET['page'])) {
      $_GET['page'] = 'main.php';

    }
    switch ($_GET['page']){
      case 'main.php':
      case 'about.php':
      case 'portfolio.php':
      case 'tips.php':
        $file = $_GET['page'];
        break;
      default:
        $file = '404.html';
    }
    include_once $file;

is it possible to get this too from the ini file? 是否有可能从ini文件中获得此信息?

Try this: 尝试这个:

$page = isset($_GET['page']) ? $_GET['page'] : "main.php";
if( file_exists($page)) include($page);
else include("404.html");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM