How can I insert some PHP coding into my CSS doc?

Discussion in 'CSS' started by mrShrimp, Apr 24, 2012.

  1. #1
    My question is the title, and the code i have so far (in a .css file) is this:

    div.element1 h2.class, div.element1 p.class, div.element1 div.class {
    margin-left:<?php echo get_the_post_thumbnail( $size[1] ); ?>px;
    }

    What I am trying to accomplish is to resize the margin in accordance to the X factor of the post_thumbnail's size. The size is stored in an array, so I figured I could call $size[1] for the X value. Can someone help me get this fixed up? Thanks! :)

    PS: I have maybe 4 hours under the belt of reading about php and have not actually spent any substantial time working with it. This is why my question may seem newbish.
     
    Last edited: Apr 24, 2012
    mrShrimp, Apr 24, 2012 IP
  2. hdewantara

    hdewantara Well-Known Member

    Messages:
    538
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #2
    Hi mrShrimp,
    I wonder how too... Could a way below be legal?

    <link rel="stylesheet" type="text/css" href="css.php?param1=<?php echo get_the_post_thumbnail( $size[1] ); ?>" />
    PHP:
    And then in the CSS.PHP file, this might be declared:
    
    div.element1 h2.class, div.element1 p.class, div.element1 div.class {
      margin-left:<?php echo $_GET['param1']; ?>px;
    }
    PHP:
     
    hdewantara, Apr 25, 2012 IP
  3. mrShrimp

    mrShrimp Peon

    Messages:
    24
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I don't know, I'll try it out today.
     
    mrShrimp, Apr 25, 2012 IP
  4. mrShrimp

    mrShrimp Peon

    Messages:
    24
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    On my page, this shows:
    " />
    It's the last piece of the link tag:
    <link rel="stylesheet" type="text/css" href="css.php?param1=<?php echo get_the_post_thumbnail( $size[1] ); ?>" />
    PHP:
     
    mrShrimp, Apr 25, 2012 IP
  5. exposingfreaks

    exposingfreaks Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    any1 know good tutorials for learning css?
     
    exposingfreaks, Apr 25, 2012 IP
  6. hdewantara

    hdewantara Well-Known Member

    Messages:
    538
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #6
    My bad coding. Sorry, I was in a rush...

    Here's an example. This changing background color app would only accept text colors, e.g. red, blue, cyan, etc. but not #FF4411: http://www.smokingscript.com/test/v1/test.php

    test.php:
    <?php
    	$backCol = $_POST['backCol'];
    	if (strlen($backCol)==0) {
    		$backCol = 'red';
    	}
    	
    	function getBackCol(){
    		global $backCol;
    		return $backCol;
    	}
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    	<title>test</title>
    	<meta content="text/html; charset=utf-8" http-equiv="content-type" />
    	<link rel="stylesheet" type="text/css" href="css.php?param1=<?php echo getBackCol(); ?>" />
    </head>
    <body>
    	Hello there!
    	<form action="test.php" method="post">
    		<label for="backCol">Change background to:</label>
    		<input id="backCol" name="backCol" type="text" value="<?php echo $backCol; ?>"/>
    		<input type="submit"/>
    	</form>
    </body>
    </html>
    
    PHP:
    css.php:
    <?php header('Content-type: text/css'); ?>
    body{
    	background-color: <?php echo $_GET['param1']; ?>;
    }
    
    PHP:
     
    hdewantara, Apr 26, 2012 IP
  7. mrShrimp

    mrShrimp Peon

    Messages:
    24
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks a bunch, I got it to work! Something weird is still happening though. I'm using this code to get the image width:

    
    <?php $fimage_data = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "thumbnail" ); ?>
    <?php $fimage_width = $fimage_data[1]; ?>
    
    <link rel="stylesheet" href="http://www.survivorsite.org/wp-content/themes/custom-community/dynamicstyle.php?param1=<?php echo $fimage_width; ?>" type="text/css" media="screen" />
    
    PHP:
    Along with your code, this puts in a margin. The last standing problem is that the margin isn't actually the width of the featured image. The code seems to read the image size as its original size, a thumbnail (150x150), but somewhere along in my archive.php file I resize it. It doesn't seem to be picking up this resized image width. What can I do to fix this?
     
    mrShrimp, Apr 26, 2012 IP
  8. hdewantara

    hdewantara Well-Known Member

    Messages:
    538
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #8
    I don't know that...
    What did your browser's Error-Console report to you,
    anything that specifically related to dynamicstyle.php ?

    Hendra
     
    hdewantara, Apr 27, 2012 IP
  9. mrShrimp

    mrShrimp Peon

    Messages:
    24
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I don't think there are any errors. I'm new to this, so I'm not exactly sure how to check for errors. I did use firebug to check for any thing in the errors section but nothing popped up, so I'm assuming the php is running fine.
     
    mrShrimp, Apr 28, 2012 IP
  10. mrShrimp

    mrShrimp Peon

    Messages:
    24
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Just fixed it actually. Thank you so much hdewantara! You saved me hours of reading up on php, not that I won't learn it someday though. :)
     
    mrShrimp, Apr 28, 2012 IP
  11. merj777

    merj777 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    try to search the w3schools for CSS tutorial..
    i hope it will help you too.
     
    merj777, Jun 15, 2012 IP
  12. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #12
    To me this looks like using PHP to do something that's got absolutely nothing to do with what PHP or CSS are for... I'd have to see the markup and what you are trying to do to it to truly weigh in, but as a rule of thumb if you're trying to generate CSS using a scripting language, there's either something horrifically wrong with your layout, or how that layout is being built.
     
    deathshadow, Jun 19, 2012 IP