简体   繁体   English

从根文件夹主页无法正确显示header.php

[英]header.php not displaying properly from root folder homepage

I have the index.php in my public_ root destination which has the header taken using php include from the folder include/header.php. 我的public_根目标目录中有index.php,其中的头文件是使用include / header.php文件夹中的php include来获取的。 But when the index page loads the menu and images within the header.php doesnt load because its directory is from include folder. 但是,当索引页加载菜单时,header.php中的图像不会加载,因为其目录来自include文件夹。

Ex in header.php the menu will be in directory 例如在header.php中,菜单将位于目录中

        <link rel="stylesheet" href="../scripts/menu.css" type="text/css" media="all" />

but the index.php directory to menu stylesheet would be scripts/menu.css. 但菜单样式表的index.php目录为scripts / menu.css。 So i guess the menu styles are not loading. 所以我想菜单样式没有加载。

Is there a way to get the index page right without having to have header in the same folder as index.php or is it because i am testing this on windows using xampp? 有没有一种方法可以使索引页正确而不必在与index.php相同的文件夹中包含标头,或者是因为我正在使用xampp在Windows上对其进行测试?

Let me clear this one up for you. 让我为您解决这个问题。

When you write HTML and CSS from PHP like you are doing here, the HTML and CSS don't know where the PHP is being run from or anything about the PHP. 当像在此处一样从PHP编写HTML和CSS时,HTML和CSS不知道从何处运行PHP或有关PHP的任何信息。 This is because PHP is interpreted and writes out the page into HTML which is then processed in your browser (ie the PHP never reaches your browser, all it sees is HTML). 这是因为对PHP进行了解释,并将页面写成HTML,然后在您的浏览器中对其进行处理(即PHP永远不会到达您的浏览器,它看到的只是HTML)。

This means that you must add the correct paths to the CSS files relative to your web server root not the PHP script location (in your case that is probably "/scripts/menu.css" for the example you've provided - assuming that the scripts is in the root web folder). 这意味着您必须相对于Web服务器根目录而不是PHP脚本位置向CSS文件添加正确的路径(在您的情况下,对于您提供的示例,该路径可能是“ /scripts/menu.css”-假定脚本位于根Web文件夹中)。

So for example, on your website / means the root of the web server (ie mywebsite.com/) and anything after that is it's path relative to the root of the web server. 因此,例如,在您的网站上/表示Web服务器的根目录(即mywebsite.com/),其后的任何内容都是相对于Web服务器根目录的路径。

By using /mycssfolder/mycssfile.css like I suggested, you can ensure that the location is correct regardless of the page which the PHP script is called on. 通过像我建议的那样使用/mycssfolder/mycssfile.css,无论调用PHP脚本的页面是什么,都可以确保位置正确。


Hope this helps. 希望这可以帮助。

Ben

<link rel="stylesheet" href="/scripts/menu.css" type="text/css" media="all" />

You might try simple removing the .., like shown above. 您可以尝试简单地删除..,如上所示。 Remember, any external source file that you include becomes part of the destination. 请记住,您包含的任何外部源文件都将成为目标的一部分。 Thus, think of the location of your CSS file relative to index.php. 因此,请考虑一下CSS文件相对于index.php的位置。

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

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