【问题标题】:Using include from a different folder使用来自不同文件夹的包含
【发布时间】:2019-02-03 23:52:53
【问题描述】:

我有一个使用 php 的项目;我有一个包含文件夹,其中有我的 header.php 和 footer.php 我的样式表和其他样式表链接在 header.php 的 head 标签中

我有另一个名为calculator的文件夹,现在我在calculator文件夹的索引文件中包含了header.php和footer.php,它可以工作,但是css没有生效

这是计算器索引文件中的代码

<?php
    include '../include/header.php';
?>

    <form action="" method="post" enctype="multipart/form-data">
        <div class="form-group">
            <label for="exampleFormControlInput1">Name of Share</label>
            <input type="text" class="form-control" id="exampleFormControlInput1">
        </div>
        <div class="form-group">
            <label for="exampleFormControlInput1">Number of Shares</label>
            <input type="text" class="form-control" id="exampleFormControlInput1">
        </div>
        <div class="form-group">
            <label for="exampleFormControlInput1">Purchase Price</label>
            <input type="text" class="form-control" id="exampleFormControlInput1">
        </div>
        <button type="submit" class="btn btn-primary">Submit</button>
    </form>


    <?php
    include '../include/footer.php';
    ?>

请指教

【问题讨论】:

  • 看看$_SERVER['DOCUMENT_ROOT']
  • 您的 css 文件路径(您在 header.php 中声明它对吗?)不正确。
  • 在样式的 href &lt;link href="/css/styles.css"..&gt; 中使用斜线 /。这将始终从服务器的(文档)根目录加载。

标签: php web


【解决方案1】:

根据我的经验,包含这样的 js 和 css 文件的最佳方式。

例如: 假设css在asset里面,asset的路径就像localhost\PROJECT\asset\css\style.css

建议创建一个全局变量:

$path = 'http://localhost/PROJECT/'; 

在你设置样式表的头文件中是这样的:

<link rel="stylesheet" href="<?php echo  $path; ?>asset/css/style.css" >

【讨论】:

  • 感谢@menaka,但在 css 链接中添加 / 无效
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-22
  • 2016-05-23
  • 1970-01-01
  • 2019-12-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多