Php - CSS elements wont load - php

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">

Related

Using a base href that will work locally and on server

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"/>

Website styling not being applied. (Calling stylesheet.css from a php include)

I wonder whether anyone could help me resolve some problems I'm having in creating a website using HTML, CSS...and PHP for the first time. (My previous attempts at web design were only in HTML and CSS).
The problem at present is that my home-page (index.php) somehow isn't 'seeing' my stylesheet.css.
The code for the index.php is basically as follows :
<?php
$page_title='Home';
[php-code here, to call in include1.php.....Please see below for details]
?>
<div class="copy">
[page content here, in html]
</div>
<?php
[php-code here, to call in include2.php.....Please see below for details]
?>
My folder structure is :
web
css
stylesheet.css
images
logo.png
includes
include1.php
include2.php
index.php
In attempting to call in include1.php (containing doc type declaration, and Head section including reference to stylesheet.css), I've tried the following (inserted between <?php and ?>, as shown above), all without success :
$pathtoinclude1 = $_SERVER]'DOCUMENT_ROOT'];
$pathtoinclude1 .= "/includes/include1.php";
include_once($pathtoinclude1);
and
include('/includes/include1.php')
In include1.php, my reference to the stylesheet.css is :
<link rel="stylesheet" href="../css/stylesheet.css" media="Screen" type="text/css"/>
When I preview the home-page, all I get is the text in default font (Times New Roman?). None of the styling via CSS is being applied.
Can anyone give me some pointers as to what I'm doing wrong?
If that first answer doesn't work, try replacing
<link rel="stylesheet" href="../css/stylesheet.css" media="Screen" type="text/css">
with <?php include 'style.php'?>
And then in the style.php file include the <style> tags and then add the css regularly.
Since you say you are using php for the first time, make sure you have the correct html declaration.
although you have already worked with html,css just a reminder:
<?php
$page_title='Home';
[php-code here, to call in include1.php.....Please see below for details]
?>
<!DOCTYPE html>
<html>
<head>
<!-- MAKE SURE YOU'RE INCLUDING THE
EXTERNAL CSS FILE HERE BUT NOT TO INCLUDE YOUR PHP INCLUDES!
WHICH ACCORDING TO YOUR FILE STRUCTURE SHOULD BE -->
<link rel="stylesheet"
href="../css/stylesheet.css"
media="Screen"
type="text/css"/>
</head>
<body>
<div class="copy">
[page content here, in html]
</div>
<?php
[php-code here, to call in include2.php.....Please see below for details]
?>
</body>
</html>
When you include() the file from index.php, the path looks like:
<link rel="stylesheet" href="../css/stylesheet.css" media="Screen" type="text/css"/>
This path is going one directory back from index.php which is not a valid path. Your path in include1.php should look like this when you include it in index.php:
<link rel="stylesheet" href="css/stylesheet.css" media="Screen" type="text/css">
Also, if CSS includes properly but styles still do not show up, try removing browser cache.
Try this. It worked for me
Let's assume that you CSS file is named 'css.css'
And it is located in the same directory with you home page.
Just add this at the head tag:
Head
Style
<?php include('css.css') ?>
style
head
Don't forget to add corresponding tags

Zend Framework 1.12 - AppendStylesheet makes link to CSS file twice

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.

CSS only working inline with PHP API

For some reason my CSS is only working when inline. I'm somewhat new to web design and not really sure about how to track down why this is occurring. It's not like I can open the console to check for errors, so what kinds of things should I look for? I'm sorry I'm not being more specific, I'm just really not even sure what to do.
Say the url is "www.example.com/testing/3"
The page testing.php would load, call API.php and determine what to do when on page testing.php with vale 3. API.php would then call a function on testing.php to deal with whatever that function tells it to do. So basically it calls out, determines which function to handle the URL, then calls back in.
When I include the CSS in the head of testing.php it works. When I just have a link, it doesn't. All of the files are in the same folder and I'm developing on localhost, no files are admin-restricted: I'm the admin anyway so permission isn't an issue.
//test.css
P.special {
color:green;
border:solid red;
margin-top:85px;
}
Head of testing.php
<link a href="test.css" rel="stylesheet" type="text/css"/>
A function on testing.php
function view_event($event_id){
?>
<p class ="special">
//stuff here
</p>
<?
}
<link a href="test.css" rel="stylesheet" type="text/css"/>
would need to be changed to:
<link a href="/test.css" rel="stylesheet" type="text/css"/>
Using relative URLs will not work when you traverse into other directories. Setting it to an absolute URL with / will allow it to work anywhere on site.
You can check the generated page source in your browser and make sure the CSS link is valid and loads the intended CSS file.
Web Developer Tools built into your browser (or as plugins) are very helpful in debugging html, css and js. Get to know them well.

PHP Page-Specific Scripts and Styles Not Recognized

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.

Categories