I have a site built with PHP includes. For scripts and styles that are used on only one page, I call them with a variable. So the header include contains this line:
<?php echo $additional; ?>
And a particular page that requires additional scripts and styles contains these lines:
<?php $additional = '<link href="/assets/style_tabbedpanels_calendar.css" rel="stylesheet" type="text/css" /><link href="/assets/style_calendar.css" rel="stylesheet" type="text/css" /><script src="/assets/script_tabbedpanels_t-l.js" type="text/javascript"></script><script src="/assets/script_calendar.js" type="text/javascript"></script><script src="/assets/script_yahoo-dom-event.js" type="text/javascript"></script>'; ?>
I didn't break each style and script onto a new line because I found that causes problems. Generally, this has worked fine, but the calendar and yahoo-dom-event scripts that I just added aren't running at all. I know this because everything works if I put them directly in the header include.
So I was wondering if I'm doing something wrong when defining the additional variable on the individual pages?
Thank you.
The problem seemed to be the order of the scripts. When I moved the yahoo one in front of the others, it started working.
Related
Following on from a question I asked yesterday that I ended up finding a solution for, I've now run into another problem that I've been trying to solve with no luck.
I'm testing php files with XAMPP and in order to allow stylesheets in header.php to work everywhere, I used the following line in the header.php file.
<base href="http://localhost/folder-name-here/">
It works while I'm testing locally, however, I'm working on a site cleanup for someone else as practice (I'm a beginner) and I've already assumed that this won't work if the site is pushed to the server - the person I'm working with has also asked why I don't use something relative to the root.
I've seen various suggestions online, like using:
<base href="~/>
or
<base href="/">
but unfortunately, none of these are working.
Not sure of what else I could try at this point. Any ideas?
Instead of the base tag i would use $_SERVER['HTTP_HOST'], this returns the base url from your site (localhost) or when it is online the website (example.com).
I picked the link from your previous post and modified it.
<link rel="stylesheet" type="text/css" href="<?php echo $_SERVER['HTTP_HOST']; ?>/assets/sf.css"/>
Ofcourse if this is not working as you like you could also create a php variable for it that you modify
<?php
$baseUrl = 'localhost/folder';
$baseUrl = $_SERVER['HTTP_HOST'].'folder';
?>
<link rel="stylesheet" type="text/css" href="<?php echo baseUrl; ?>/assets/sf.css"/>
this way it is easy to adjust all the links at once when you deploy to a webserver
edit:
for when discussion chat room closes this was the solution that kinda worked,
<link rel="stylesheet" type="text/css" href="//<?php echo $_SERVER['HTTP_HOST']; ?>/kinderwunschpraxis_site/assets/sf.css"/>
<link href="/app/app.css" rel="stylesheet" type="text/css">
The above is the code I'm using to access a stylesheet, but it's not working at the .
Some extra details:
This is in a .php file, but it's located within the head of an html section
I'm working on a temporary url (i.e. 'my.ipa.dd.res/mydomain.com/dir/'). This might be the reason it's not working.
Edit:
It's a stylesheet I'd like to use on several pages, which is why I'm trying to point to a root directory (so that I don't need the file in every single folder I create).
Well I think you need to store your root directory path as a string to include your css file with an absolute URL.
Something like :
<link href="{$absoluteRootPath}/css/styles.css" rel="stylesheet" type="text/css">
If you remove the leading slash it will look for the css file in the folder relative to the current.
<link href="app/app.css" rel="stylesheet" type="text/css">
Edit: to reuse in multiple scripts in different dirs you would need to specify an absolute path, so to avoid having to change it in multiple places when you go live (ie stop using the temporary url) you should define a variable.
<?php
// set absolute path to the stylesheet
$cssPath = 'http://my.ipa.dd.res/mydomain.com/dir/';
?>
And
<link href="<?php echo $cssPath; ?>app/app.css" rel="stylesheet" type="text/css">
Depending on your php architecture you may need to define $cssPath as a global or you may be able to add it to an existing config variable. This is completely dependent on your framework.
I've been having this same problem recently. Here's what worked for me
Now on the index page, replace link rel="/app/app.css" href="stylesheet" type="text/css"
with
?php include 'style.php' ?
And on the style.php page insert the following tags:
style and /style
You can put the regular CSS between those two tags. See if that works. BTW for some reason I can't insert the greater or less than symbols without making the code disappear... Forgive me, I'm new here..
I am new to this board and since I sadly couldn't find the answer while searching for it, I thought I should post it here :P
Enough from me, I've got the following problem:
I made a website (for practice) and tried to make a guestbook in it.
Worked out pretty well so far, yet I have one big problem. As soon as I try to get from one .php page to another .php page and include a HTML page on there, it wont load the CSS elements anymore.
When going from a .html page to a .php page and including a HTML page, it works perfectly fine.
I've also checked if the path are correct, and if this bug appears on more than just that one .php page, and yea, it made always problems in that scenario.
Here are the include and stylesheet I am using (I hope that's all you need as information)
include('gaestebuchAfterLogin.html');
<link rel="stylesheet" href="../../css/style.css" type="text/css">
Edit:
here is some more code:
<?php
session_start();
?>
<?php
if(isset($_SESSION["login"]))
{
include('gaestebuchAfterLogin.html');
}
else
{
include('gaestebuchFail.html');
}
?>
<?php
session_start();
?>
^ one PHP Element
<?php
if(!isset($_SESSION["username"]))
{
echo "Bitte erst einloggen";
exit;
}
?>
^ the second PHP Element (for example)
well, as said, the include works perfectly fine, when i haven't been on a .php page before. But if i have been on a php page before, it wont work anymore
Edit2:
sorry for posting in answers and thanks for the quick responses :P
Anyways, tried to use the absolute path now, did not work out, too
try and use absolute paths instead, change:
<link rel="stylesheet" href="../../css/style.css" type="text/css">
into:
<link rel="stylesheet" href="/css/style.css" type="text/css">
This:
<link rel="stylesheet" href="../css/style.css" type="text/css">
Or create a path variable from root using $_SERVER to get your url.
Define it and then include your php in the page.
$app_url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/';
define("APP_URL",$app_url);
and then
<link rel="stylesheet" href="<?php echo APP_URL?>css/style.css" type="text/css">
I am doing the examples in the book "Zend Framework - A Beginner's Guide". I am almost done but I have found a few weird issues. One of them is in my admin.phtml layout file. I have this code:
<?php echo $this->headLink()->appendStylesheet($this->baseUrl().'/css/master.css'); ?>
When I load the page I see it displays the master.css link twice:
<link href="/square2/public/css/master.css" media="screen" rel="stylesheet" type="text/css" />
<link href="/square2/public/css/master.css" media="screen" rel="stylesheet" type="text/css" />
When I comment out the appendStyleSheet() function, as expected, nothing shows up. Any reason why this function is doing it twice when I only make the call once?
Addendum: As a test I did two appendStyleSheet functions and in the resulting page I see the CSS file three times, apparently twice for the first call and once for the second. Any clues?
Just echo headLink
<?php $this->headLink()->appendStylesheet($this->baseUrl().'/css/master.css'); ?>
<?= $this->headLink(); ?>
I fixed this by using setStylesheet() instead:
<?php $this->headLink()->setStylesheet($this->baseUrl().'/css/master.css'); ?>
As explained by Joel Joel Binks, the first call to appendStylesheet() appends the stylesheet and returns it. The second call returns any previously appended stylesheets plus the second stylesheet hence the first stylesheet will appear twice.
<?php
// This is index.php
ob_start();
include 'tem/u.html';
ob_end_flush();
?>
<html>
<!-- This is u.html -->
<link rel="stylesheet" href="style.css" media="screen" />
<body>
<p> abc </p>
</body>
</html>
Now my problem is when i run h.html -> Ok with style.
But when i run index.php -> Ok without style (because now the index.php include style.css, not tem/style.css)
Need a fix
If possible, refer to a domain relative path to the style.css, for example
<link rel="stylesheet" href="/style.css" media="screen" />
If that is not possible, you need to keep track on the page base in some way, which I cannot tell because I do not know enough about your application. But anyway, like
<link rel="stylesheet" href="<?php echo $pageBase; ?>/style.css" media="screen" />
where $pageBase is a variable containing the url to the root of your application.
I'm assuming that the tem directory is supposed to be for some sort of template, and so you don't want it to be directly exposed to the user; rather, you want to be able to include the files so that they're accessible via index.php, possibly with the option of later changing what files are included.
You could create another PHP file called style.php (in the root directory) which would include tem/style.css. You could do this for any other files that your templates used as well — the idea being that each PHP file in the root directory would correspond to a "role" in the template, not a particular template file, so that the template could later be changed without everything needing to be rewritten.
This might get a bit cumbersome if you had a lot of files required by your template, so it might be better to have a single script that could be instructed which file to load (through a $_GET variable). But in that case, you need to be very careful not to allow the user to specify arbitrary files. I'd suggest avoiding this approach until you're more proficient in PHP.
EDIT: On second thought, I'd suggest using a <base> tag in your template HTML file, as suggested in my comment on #gnud's answer.
This has nothing to do with PHP or include. This has to do with your browser, and how URLs are interpreted.
When your browser is pointed at http://xyz.abc/tem/h.html and asked to load "style.css", it tries to load http://xyz.abc/tem/style.css - this is known as a relative url, relative to the current document location.
When your browser is at http://xyz.abc/index.php and is asked to load the stylesheet in the same way, it tries http://xyz.abc/style.css. Maybe you see the problem?
As for a solution, you might use a domain-relative path for the stylesheet ("/tem/style.css").
just always use absolute path to your css file
<link rel="stylesheet" href="/tem/style.css" media="screen" />
that's all