Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何创建一个主php文件,以便其他php文件可以填补空白?_Php_Html - Fatal编程技术网

如何创建一个主php文件,以便其他php文件可以填补空白?

如何创建一个主php文件,以便其他php文件可以填补空白?,php,html,Php,Html,我正忙于练习如何创建网站。现在我想用一种适合我的方式来做。但目前我无法创建一个主页,以便以后能够填充空白。标题很简单,我成功地在不同的页面上设置了标题。但是当我试图设置整个页面的内容时,我失败了 这将是我填写空白的页面: <!doctype html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link

我正忙于练习如何创建网站。现在我想用一种适合我的方式来做。但目前我无法创建一个主页,以便以后能够填充空白。标题很简单,我成功地在不同的页面上设置了标题。但是当我试图设置整个页面的内容时,我失败了

这将是我填写空白的页面:

<!doctype html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <title><?php echo $title ?></title>
    <?php require 'navigation.php'; ?>
</head>
<body>
    <div class="content">
        <?php $content ?>
    </div>
    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
</body>
<?php require 'footer.php'; ?>
</html>
下面的代码将是我的索引页。如果我像下面那样设置$content,它会工作,但我不满足于此解决方案,因为等号后面的部分将被视为字符串,它感觉不整洁

<?php $title = "Home";?>
<?php $content = "<div class='container bg-image'></div>" ?>


<?php require '..\resources\views\main.php';?>

所以我的问题是我能用更好的方式来做吗?我该怎么做呢?

您应该使用在另一个php文件中包含一个php文件

在学习如何编写php的这个阶段,我建议根据您在页面上的位置包含文件

但在这样做之前,我想指出的是,您并不是在重复代码中的内容。您编写了,但应该为当前方法编写

现在,回到我的建议: 让我们来看看这个文件结构:

/content/home.php
/content/about.php
/includes/navigation.php
/index.php
“内容”文件夹中的每个文件都可以只包含原始HTML,您希望将其显示在当前的位置

好的,让我们打开index.php

<?php
// Content of index.php
$defaultpage = 'home';

// We would like to display different pages based on the url
// Use the global get variable to figure out which page we want to display
$page = isset($_GET['page']) ? $_GET['page'] : $defaultpage;
// Here i used a short syntax for if and else. This code above is equal to:
//
// if (isset($_GET['page'])) {
//    $page = $_GET['page'];
// } else {
//    $page = $defaultpage;
// }

// Hardcoded list of available pages + the title each page should show
$pages = [
    'home' => [
        'title' => 'Welcome to my site',
        'file' => 'home.php'
    ],
    'about' => [
        'title' => 'About me',
        'file' => 'about.php'
    ]
];

// See if the requesed page is a available page by looking inside $pages
if (isset($pages[$page])) {
    $page = $pages[$page];
}
else {
    $page = $pages[$defaultpage];
}
// $page is now overwritten with an associative array containing
// both 'title' and 'file' for content.

// Let us prepare a variable containing the requested page.
$file = dirname(__FILE__)."/contents/".$page['file'].".php";

// If the file doesn't exist, stop execution with an error message
if (!file_exists($file)) {
    exit('<h1>Page not found');
}
?>
<!doctype html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <title><?php echo $page['title']; ?></title>
    <?php require 'navigation.php'; ?>
</head>
<body>
    ...
    <div id="content">
    <?php include($file); ?>
    </div>
    ...
</body>
</html>

我不知道您为什么将导航文件包括在head标签中,但我认为您只是把它放错了地方。

这可以使用,我想它会有用的

    <?php
      include('test.php') //this is how you can add an external php 

      /*but its recommended to use require rather than include as it                                
      throws error to the browser which is really a serious threat*/

     require('test.php')

   ?>
祝你一切顺利


你知道你忘记了吗两次?这是解决我问题的最好办法。这正是我想要的。我只有一个问题,为什么你认为我的导航文件放错地方了?它只是我的导航栏所在的文件,通常导航栏会放在头部,对吗?如果我错了,请纠正我。应该包含将在网站上看到的所有内容。应包含标签等。加载og样式文件和脚本也是head的一项重要功能,但脚本标记通常放在末尾,因此在脚本运行时加载所有内容。