Jump to content

[PHP/CSS/General WebDev-ery] Referencing variables contained in a separate file?


Recommended Posts

Posted

So let's say I'm coding a web page. I've figured out using an external stylesheet with , but I'm stuck with referencing variables.

 

To try and figure this out I just have a simple variable, $Header.

The StyleSheet defines $Header = 'Hello World';

 

However if I include <?php echo $Header; ?> I get PHP Notice: Undefined variable: Header in [...]\index.php on line 7

 

(Ideally the end-game has me with a language file separate to the StyleSheet containing a list of strings but I'm not fussed about separating it over multiple files right now whilst I'm just getting to grips with everything.)

Posted (edited)

Is this for variables within CSS or just standalone?

 

Normally the way you're doing it via the CSS is for pushing colours etc between the files on the fly with the variable.

 

If you just want a standalone string file just use a straight include of the php file, and then reference the variable.

 

ExternalFile-
$Header = 'Hello World';

===============================

MainFile-
include 'ExternalFile.php';
echo $Header;

 

Steve

Edited by Steve21
Posted (edited)
Is this for variables within CSS or just standalone?

Not really sure what you're asking there. This is my first time diving into something like this.

 

my index.php contains:




	
	


 

and my config\localisation\en_gb.php contains:

$Header = 'Test String Header';
$Days = array["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
$Months = array["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

 

and I'm still getting Undefined Variable.

Edited by Garacesh
Posted

Aha, figured it out..

 

en_gb.php needed to be

<?php

$Header = 'Test String Header';

$Days = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");

$Months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

?>

 

(On another note.. I thought PHP was supposed to use array[] after PHP5, but, apparently not as it was throwing an error saying found [ expected (...)

Posted
In the OP, you are linking to a php script using the tag. This in turn means that when the page loads it will go through the following process,

[snip]

If you are hoping to have $Header inside stylesheet.php and have that usable as a variable inside .php then PHP is what needs to include it, so swapping the for the following may suffice:

I've decided to go about it a different way. It feels a little convoluted, but I think it will work out for the best in the end once I'm done and I release it (if all goes to plan, I'll be giving it away on GPL once I'm done)

 

[Root Folder]
GlobalConfig.php
[config]
	style.php
	[localisation]
		en_gb.php
[img]
	logo.png
[web]
	index.php
	bookings.php
	login.php

 

GlobalConfig contains all the stuff like LDAP/SQL info, stylesheet variables (font face, colours, etc) and language file location. The working theory is that the stylesheet itself doesn't need editing, so the stylesheet can just exist on its own in the config folder and not get touched. This way, GlobalConfig the only file that will need editing (unless you want to translate all the strings in en_gb.php to another language).

Posted

Why do you have stylesheet.php as a php file instead of a static CSS file? I can only assume it's because individual users are intended to be able to do customisations?

 

If that's not the case, consider just having a static .css file instead - too often I see static assets being served by PHP etc when a static text file is more efficient :) If you are looking for templating then have a look at LESS or SASS, as then you can use variables in the CSS file and just generate the final CSS file at the end, including minification etc. to make it faster for users to load.

Posted
Why do you have stylesheet.php as a php file instead of a static CSS file? I can only assume it's because individual users are intended to be able to do customisations?

 

If that's not the case, consider just having a static .css file instead - too often I see static assets being served by PHP etc when a static text file is more efficient :) If you are looking for templating then have a look at LESS or SASS, as then you can use variables in the CSS file and just generate the final CSS file at the end, including minification etc. to make it faster for users to load.

 

Good point. It's just a leftover from when I was storing variables in the stylesheet itself, but there's no reason it needs to be .php any more.

 

I have zero plans of having end-users individually modify their stylesheet. Sure, it could probably be done with cookies, but I'm concerning myself with core functionality at the moment. I'm just focussing on having a static stylesheet defined and getting LDAP and SQL working.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...