3 Replies - 1222 Views - Last Post: 26 October 2010 - 03:57 AM Rate Topic: -----

#1 Vampyress  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 25-October 10

Dynamic CSS with PHP/MYSQL problem

Posted 25 October 2010 - 05:56 PM

Hey guys,

I'm currently working on a project which will require a dynamic CSS file. I've decided that the best solution for this would be to create the CSS using PHP however I need the values to be retrieved from a database. The reason for this is that the information in the CSS file will be updated regularly based on user preferences. In order to make sure this is possible i've been conducting a series of small scale tests before developing my final product.

<?php
header("Content-Type: text/css");

// database connection

$result = mysql_query ("select css_value from test_data where css_id= '1' ");

$row = mysql_fetch_array($result);
$color = $row[css_value];

//$color = 'red';

?>

#test {
color: <?php echo $color; ?>
}

<?php
mysql_close($conn);
?>



When I manually set the $color variable then the CSS works exactly as desired, however any attempt to retrieve the variable from the database has no effect (i've tested that the variable can be retrieved). I'm curious as to whether what i'm attempting is possible or whether there has been a serious syntax error on my part (which is very likely as it's 2am and my brain is mushy from the flu). And I do appologise in advance for how silly this question may be if it is a syntax error, I have a bad habit of making really basic mistakes and spending hours stuck over them!! :P

If anybody could give me some advice that would be greatly appreciated!

regards,

Leanne

Is This A Good Question/Topic? 0
  • +

Replies To: Dynamic CSS with PHP/MYSQL problem

#2 Vampyress  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 25-October 10

Re: Dynamic CSS with PHP/MYSQL problem

Posted 25 October 2010 - 06:09 PM

Sods law that I would find the solution myself, and it was a syntax error afterall! It seems I wanted to mysql_fetch_row rather than array :)

<?php
header("Content-Type: text/css");

//database connection

$result = mysql_query ("select css_value from test_data where css_id= '1' ");

$row = mysql_fetch_row($result);

mysql_close($conn);
?>

#test {
color: <?php echo $row[0]; ?>
}



This post has been edited by Vampyress: 25 October 2010 - 06:09 PM

Was This Post Helpful? 1
  • +
  • -

#3 Fratyr  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 139
  • Joined: 10-April 08

Re: Dynamic CSS with PHP/MYSQL problem

Posted 26 October 2010 - 03:51 AM

Why you do It that way? Ok, I accept html+php, but css!?!??

Can't you generate .css file on backend, give It a revision as timestamp and give It to your frontend?
Runtime generation with queries and combining PHP+CSS It's very very bad!
Was This Post Helpful? 0
  • +
  • -

#4 PhunkRabbit  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 81
  • View blog
  • Posts: 406
  • Joined: 23-August 10

Re: Dynamic CSS with PHP/MYSQL problem

Posted 26 October 2010 - 03:57 AM

Grats on solving the problem Vamp. Early hours and scripting is an easy way to miss obvious errors.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1