Page 1 of 1

Simple question related to php and css

Posted: Fri Dec 18, 2009 5:47 pm
by groc426
The problem:
I'm trying to get a div the same width as my image in a dynamic manner.

So here is the the code:

Code: Select all

 
<?php
  $my_image = array_values(getimagesize('../images/thumb/DSC_0488.jpg'));
  list($width) = $my_image;
?>
 
It gets the width fine. Now I want to implement that in an inline style, but I'm uncertain of the syntax.
This is what I thought would work:

Code: Select all

<div class="bottomdiv" style="width: <?php $width?>;">
 
So how would I add the width style with the variable I produced? Thank you all!

Re: Simple question related to php and css

Posted: Fri Dec 18, 2009 5:55 pm
by AbraCadaver
Maybe this, assuming that the page is being parsed by PHP (most likely a .php extension):

Code: Select all

<div class="bottomdiv" style="width: <?php echo $width; ?>">

Re: Simple question related to php and css

Posted: Fri Dec 18, 2009 5:58 pm
by MichaelR

Code: Select all

 
 
$my_image = getimagesize('../images/thumb/DSC_0488.jpg');
 
...
 
<div class="bottomdiv" style="width: <?php echo $my_image[0]; ?>">
 
 
Edit: Seems I was beaten to it.

Re: Simple question related to php and css

Posted: Fri Dec 18, 2009 6:02 pm
by AbraCadaver
I would use MichaelR's if I were doing it. I just gave you the quick fix. I would assume that you may want to specify the height as well, and with the array you have access to all the info to use/echo without having to change your list() arguments. I can only remember once or twice a long time ago when I've used list().

Re: Simple question related to php and css

Posted: Fri Dec 18, 2009 8:52 pm
by groc426
Strange...it still doesn't seem to be working. If I enter the width manually, it works fine. I will lay out the whole code of what I'm attempting to accomplish and maybe one of you can spot something I cannot:

Code: Select all

<?php
  //renumber
  $my_image = array_values(getimagesize('../images/thumb/DSC_0488.jpg'));
  //use list on new array
  list($width) = $my_image;
?>
<div class="gal1">
    <div class="gal2" style="height:169px;">
        <a href="/Dave/images/DSC_0488.jpg" rel="lightbox-gallery1" title="my caption">
            <img src="/Dave/images/thumb/DSC_0488.jpg"/>
        </a>
    </div>
    <div class="bottomdiv" style="width: <?php echo $my_image[0]; ?>">
      <div class="galL"></div>
    </div>
</div>
<?php echo $width ?>
I echo to make sure I'm getting the correct width. Should that not set the width? As I stated earlier if I set the width (ie "width: 179px;") it will work fine. With the PHP its inheriting the the width instead of inserting 179px. I display the width at the bottom just to be sure I'm getting the correct width. I'm confused why this is not working. Any ideas?

Re: Simple question related to php and css

Posted: Sat Dec 19, 2009 2:27 am
by cpetercarter
When you check to see that your php code outputs the correct value, does it output eg "179px" or just "179"? If the latter, then you need to add "px;" to the width attribute.

Code: Select all

<div class="bottomdiv" style="width: <?php echo $my_image[0]; ?>px;">

Re: Simple question related to php and css

Posted: Sat Dec 19, 2009 10:39 am
by groc426
Thank you so much cpetercarter! That was it. Me = :crazy: I kept attempting to concatenate the variable with .px instead of leaving it outside the variable. Lets not say how long I worked on this simple problem :oops:

Thank you all for your input and help!!