Simple question related to php and css

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
groc426
Forum Newbie
Posts: 16
Joined: Tue Oct 28, 2008 4:44 pm

Simple question related to php and css

Post 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!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Simple question related to php and css

Post 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; ?>">
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
MichaelR
Forum Contributor
Posts: 148
Joined: Sat Jan 03, 2009 3:27 pm

Re: Simple question related to php and css

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Simple question related to php and css

Post 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().
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
groc426
Forum Newbie
Posts: 16
Joined: Tue Oct 28, 2008 4:44 pm

Re: Simple question related to php and css

Post 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?
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Simple question related to php and css

Post 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;">
groc426
Forum Newbie
Posts: 16
Joined: Tue Oct 28, 2008 4:44 pm

Re: Simple question related to php and css

Post 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!!
Post Reply