Ell20 Posted January 15, 2008 Share Posted January 15, 2008 Hi, Can you use PHP and/or MySQL in CSS? If so how is this done? Thanks Quote Link to comment Share on other sites More sharing options...
monkeytooth Posted January 15, 2008 Share Posted January 15, 2008 ..... Possible: Yes How-to: Same way you would making any other site.. Quote Link to comment Share on other sites More sharing options...
Grant Holmes Posted January 15, 2008 Share Posted January 15, 2008 More details? CSS can control how PHP appears on a page. CSS will not control MySQL. you could use values from a MySQL database in CSS. What are you trying to do? Quote Link to comment Share on other sites More sharing options...
Ell20 Posted January 15, 2008 Author Share Posted January 15, 2008 I have tried to query the database in the normal way and for some reason its not working: <?php header("Content-type: text/css"); require_once ('mysql_connect.php'); include_once ('includes/functions.php'); $id = $_SESSION['club_id']; $query = "SELECT * FROM club WHERE club_id = '$id'"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); $bgcolour = $row['bgcolour']; $textcolour = $row['textcolour']; ?> body { color: <?=$textcolour?>; background-color: <?=$bgcolour?>; } Can you see anything wrong with this? Thanks Quote Link to comment Share on other sites More sharing options...
papaface Posted January 15, 2008 Share Posted January 15, 2008 Should be: <?php header("Content-type: text/css"); require_once ('mysql_connect.php'); include_once ('includes/functions.php'); $id = $_SESSION['club_id']; $query = "SELECT * FROM club WHERE club_id = '$id'"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); $bgcolour = $row['bgcolour']; $textcolour = $row['textcolour']; ?> body { color: <?php echo $textcolour; ?>; background-color: <?php echo $bgcolour; ?>; } Quote Link to comment Share on other sites More sharing options...
Ell20 Posted January 15, 2008 Author Share Posted January 15, 2008 Sorry if im being blind but I cant see anything differences between mine and your code? Thanks Quote Link to comment Share on other sites More sharing options...
trq Posted January 15, 2008 Share Posted January 15, 2008 Your code uses <?= while papaface's used the more common <?php echo Quote Link to comment Share on other sites More sharing options...
Ell20 Posted January 15, 2008 Author Share Posted January 15, 2008 It still dosent like it for some reason. In the database I have stored: '#999999' as bgcolor and '#ffffff' as text color. Thanks Quote Link to comment Share on other sites More sharing options...
trq Posted January 15, 2008 Share Posted January 15, 2008 Well, lets firstly make sure your query works before trying to use its result. This is always good practice. <?php session_start(); header("Content-type: text/css"); require_once ('mysql_connect.php'); include_once ('includes/functions.php'); $id = $_SESSION['club_id']; $query = "SELECT * FROM club WHERE club_id = '$id' LIMIT 1"; if ($result = mysql_query($query)) { if (mysql_num_rows($result)) { $row = mysql_fetch_assoc($result); $bgcolour = $row['bgcolour']; $textcolour = $row['textcolour']; } else { echo "No record found"; } } else { echo Query failed " . mysql_error(); } ?> body { color: <?php echo $textcolour; ?>; background-color: <?php echo $bgcolour; ?>; } I also noticed you where missing a call to session_start(). Quote Link to comment Share on other sites More sharing options...
Ell20 Posted January 15, 2008 Author Share Posted January 15, 2008 That dosent work either, no message displayed either. Is the possible reason for this that on the php page header.html is called, header.html then calls the CSS? The reason there is no session_start() is because the header.html includes this, which is included on every page. Thanks Quote Link to comment Share on other sites More sharing options...
trq Posted January 15, 2008 Share Posted January 15, 2008 The reason there is no session_start() is because the header.html includes this, which is included on every page. Is your server configured to handle html as php? By the way... what file extension is on the code you have posted? *.php ? Quote Link to comment Share on other sites More sharing options...
Ell20 Posted January 15, 2008 Author Share Posted January 15, 2008 The reason there is no session_start() is because the header.html includes this, which is included on every page. Is your server configured to handle html as php? By the way... what file extension is on the code you have posted? *.php ? I have no idea, before I attempted to make the changes to the CSS it was working fine. The code I have posted is newcss.php. Thanks Quote Link to comment Share on other sites More sharing options...
trq Posted January 15, 2008 Share Posted January 15, 2008 php won't work in a html file by default. But yeah, if your css doc has the php extension, I see no reason why its not working. Add this to the top. <?php error_reporting(E_ALL) ; ini_set('display_errors','1'); ?> Quote Link to comment Share on other sites More sharing options...
Ell20 Posted January 15, 2008 Author Share Posted January 15, 2008 Still nothing, no errors, the page is white with no CSS at all. This is annoying!! Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted January 15, 2008 Share Posted January 15, 2008 thorpe missed a quote. <?php session_start(); header("Content-type: text/css"); require_once ('mysql_connect.php'); include_once ('includes/functions.php'); $id = $_SESSION['club_id']; $query = "SELECT * FROM club WHERE club_id = '$id' LIMIT 1"; if ($result = mysql_query($query)) { if (mysql_num_rows($result)) { $row = mysql_fetch_assoc($result); $bgcolour = $row['bgcolour']; $textcolour = $row['textcolour']; } else { echo "No record found"; } } else { echo "Query failed " . mysql_error(); } ?> body { color: <?php echo $textcolour; ?>; background-color: <?php echo $bgcolour; ?>; } Quote Link to comment Share on other sites More sharing options...
Ell20 Posted January 15, 2008 Author Share Posted January 15, 2008 Thanks Ken, but still no luck. To answer a previous question about the php being used in html. I think it must work because when I set say $bgcolour = '#fffff'; Then use echo in the CSS it works, its just when I attempt to get a value from the database that its just going white with no CSS at all. Thanks Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted January 15, 2008 Share Posted January 15, 2008 Well you just have to have the file extension .php. You can use HTML in a .php file like so: <?php // woot! some php codes ?> <b> hi </b> See? All I did was close out the PHP and just use HTML or CSS. As long as the file extension is php, it should be fine. Quote Link to comment Share on other sites More sharing options...
Ell20 Posted January 15, 2008 Author Share Posted January 15, 2008 Yeah I understand that I was just refering to a question which thorpe asked. I have done this on my pages but it still dosent seem to like it! Thanks Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted January 15, 2008 Share Posted January 15, 2008 If by not work, do you mean the variables are empty? Because I think that's the case. Can you page source the page and find that part of the CSS and copy and paste it here? In firefox, you just press Ctrl+U, in IE, you go to Edit and Page Source. (I think, not sure since I don't use IE) Quote Link to comment Share on other sites More sharing options...
Ell20 Posted January 15, 2008 Author Share Posted January 15, 2008 If by not work, do you mean the variables are empty? Because I think that's the case. Can you page source the page and find that part of the CSS and copy and paste it here? In firefox, you just press Ctrl+U, in IE, you go to Edit and Page Source. (I think, not sure since I don't use IE) I dont see how the variables can be empty as I have manually placed them in the database. CSS dosent show up in page source, all I can see is this: <link media="screen" href="/includes/newcss.php" rel="stylesheet" type="text/css" /> Thanks Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted January 15, 2008 Share Posted January 15, 2008 Then look inside that link Quote Link to comment Share on other sites More sharing options...
Ell20 Posted January 15, 2008 Author Share Posted January 15, 2008 I went to: /includes/newcss.php And I found this: <br /> <b>Warning</b>: require_once(mysql_connect.php) [<a href='function.require-once'>function.require-once</a>]: failed to open stream: No such file or directory in <b>/home/mysport/public_html/includes/newcss.php</b> on line <b>3</b><br /> <br /> <b>Fatal error</b>: require_once() [<a href='function.require'>function.require</a>]: Failed opening required 'mysql_connect.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in <b>/home/mysport/public_html/includes/newcss.php</b> on line <b>3</b><br /> Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted January 15, 2008 Share Posted January 15, 2008 Uh oh, that's not good. What's your includes/newcss.php file? Quote Link to comment Share on other sites More sharing options...
Ell20 Posted January 15, 2008 Author Share Posted January 15, 2008 Its this file I have been working on, which I am hoping to take values from the database and put them into the CSS. Thanks Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted January 15, 2008 Share Posted January 15, 2008 Is mysql_connect.php a file on the directory? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.