Jump to content

[SOLVED] PHP and Paths in CSS


dprichard

Recommended Posts

Is there a way to set your path to image files in CSS dynamically with PHP.

 

Example:

 

Instead of this...

 

#menu a {
background: url("../images/button.jpg") 0 0 no-repeat;
}


I want to do something like this...

[code]#menu a {
background: url("<?php echo $company['company_ttracker_path']; ?>images/button.jpg") 0 0 no-repeat;
}

[/code]

Link to comment
Share on other sites

Or even better, do this:

 

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

 

And have your css.php file start with this:

 

<?

header("Content-type: text/css");

 

That way you keep your css file seperate. :)

Link to comment
Share on other sites

 

<?

header("Content-type: text/css");

 

 

 

Thanks jesirose...  I did this and am having one small issue.  In the new PHP file where do I close the PHP tag?  At the end or right after the header("Content-type: text/css");

 

I closed it right after the hearder and then used the following for the code...

 

	background-image: url("<?php echo $company['company_ttracker_path']; ?>images/dsg_portal_14.jpg");

 

But the path isn't working.  I tried closing it at the end of the PHP file, but that didn't work either. 

 

Thanks again for any assistance on this!

Link to comment
Share on other sites

okay, even though the slash with in the field in the database...  I added it and that worked.  The only weird thing now is the body margin quit working when I changed it to the PHP page.  All the Other CSS works...

 

body {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
font-weight: normal;
color: 6484ab;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
margin-left: 0px;
}

 

Link to comment
Share on other sites

Well if $company is never defined in that file, that would be why. If $company is defined in the file you were previously including the CSS in, it won't work.

 

I usually set up my files to all use one config file, where I put important stuff like file paths (as constants.) and session_start()

 

So on every page I have I would start it off by require_once('config.php'); Then in my css file I'd be able to use any vars I defined in config. This probably sounds like overkill for one or two files but it pays off in the end when you don't have to go back and edit every file to change one variable you used in them.

(Actually, I use a .htaccess file to redirect everything to a base file which includes the file then, but that is probably a bit advanced, no offense!)

 

Edit: I don't know why your margin would not work, are you using the same browser as before?

You can do margin: 0 0 0 0; to save a few lines, btw. (top, right, bottom, left.)

Link to comment
Share on other sites

No offense taken...  I was chided by a moderator to stop using Dreamweaver PHP and start writing my own so I am really working hard to figure all this out.  It has made my life a lot easier once I work through the little things like this. 

 

I have a config page now that has the database connection in it.  Do you think it is better to store stuff like the root path in that as well?  As far as constants are concerned, I haven't used them before, but do I found a little tutorial.

 

define("PATH","http://www.path.com/");

 

Then call it like this...

 

echo (PATH);

 

is that right?

Link to comment
Share on other sites

That is indeed a constant. You can do anything with it like a string so you can do

print PATH.'myfile.php';

 

And good for you for learning more on your own, it's all too easy to become dependent on something like dreamweaver and not actually understand what is going on with the code! :)

 

You can store your config file wherever you want, I guess. I have a framework I wrote so all of my pages are in pages/, my processing stuff is in actions/, etc. Then the base file is the config info (paths, db connection, etc) and then it takes the URL given (such as mysite.com/process/login/) and loads the /actions/login.php file.

Link to comment
Share on other sites

So, if I wanted to define a path on the site...  Do that as a constant.

 

For the database connection, do you just put that in the page that you are including or do you do it as a constant then echo out the constant? 

 

Also, what is the difference between doing a constant versus a function?  Sorry about all the extra questions.  I understand if you don't have time to answer and appreciate that help that you already gave. 

Link to comment
Share on other sites

A constant is like a variable except it cannot change. Using constants is basically a security thing.

A function takes arguments and is used to run the same code over and over with different arguments.

 

So here is an example of all of these (plus a class).

 

Constants: (This is the config.php file)

// Set the database type, server and credentials
define('DB_USER', 'username');
define('DB_PASS', 'pass');
define('DB_DB', 'my_db');
define('DB_SERVER', 'server');

// Establish the DB connection
$db = new DB(DB_DB, DB_USER, DB_PASS, DB_SERVER);
$db->connect();

 

Excerpt from DB.php File which is a Class

Class DB{
var $username;
var $password;
var $server;
var $database;

function DB($database, $username, $password, $server){
	$this->database = $database;
	$this->username	= $username;
	$this->password	= $password;
	$this->server	= $server;
	return TRUE;
}

function connect(){
	$rs = mysql_pconnect($this->server, $this->username, $this->password);
	if($rs !== FALSE){
		if(mysql_select_db($this->database) !== FALSE){
			return TRUE;
		}else{
			dieError('Could not select database.');
		}
	}else{
		dieError('Could not connect to server. '.mysql_error());
	}
}

function execute($sql){
	//Before doing a query, clear out the old one.
	$rs = mysql_query($sql);
	if($rs === FALSE){
		dieError('Could not complete query.<br />Query: '.$sql.'<br />Error: '.mysql_error());
		return FALSE;
	}else if($rs === TRUE){
		return TRUE;
	}else{
		$result = New Result($rs);
		return $result;
	}
}
}

 

So now $db is my variable which contains the connection and all the info in the class.

So when I want to run a query on another page, it has the config.php included in it, I just use $db.

$sql = "SELECT * from Users";
$result = $db->execute($sql);

 

I hope that actually helps and is not just confusing ;)

Link to comment
Share on other sites

Wow, i can almost read all of that...  ;D

 

Some of it makes sense and some is still a little foggy, but I get the general idea.  I am going to change some things around and give this a try.

 

What if you have multiple queries on one page?  Do you just change this $result = $db->execute($sql); to another thing like $results2 ?

 

Tell me if I sound too lost!!!

Link to comment
Share on other sites

If you still need to use $result, yes, just make it a new name. I'd name them something related.

So for my user example, I was getting all the users in the database, so that result would be $users.

Then if I want to get my...erm...items, I'd do

$sql = "SELECT * FROM items";

$items = $db->execute($sql);

 

I also have a class which handles the results, based on ADODB. If you want to use a class for your DB and don't feel up to writing your own, ADODB is a good one. But anyway here is the Result class - you're welcome to use it if you want.

 

Class Result{
var $numRows;
var $result;
var $fields;

function Result($rs=NULL){
	if($rs){
		$this->result = $rs;
		$this->numRows = mysql_num_rows($rs);
	}
}

function getRow(){
	$this->fields = array();
	if($this->numRows){
		$this->fields = mysql_fetch_array($this->result);
		return $this->fields;
	}else{
		return FALSE;
	}
}
}

 

I actually took it a step farther. Say I have another class, and I want to use $db in it to make one of it's functions access the database. Well, I don't want to have to do global $db; in every function. So I wrote One more function which is not in a class:

function dbExecute($sql){
global $db; //which is in config.php
if(($db_result = $db->execute($sql)) === false){
	$err = 'Query: '.$sql."\n".'SQL Error : '.mysql_error()."\nURL: ".$_SERVER['REQUEST_URI'];
	die($err);
}else{
	return $db_result;
}
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.