1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

dynamic image height

Discussion in 'PHP' started by John H, Aug 20, 2010.

  1. #1
    What I am working on is a background to a user input comment -- essentially a framed box around the comment. For some reason I cannot fathom but which is probably simple, I can't get the image height variable, (set by the length of the comment) to be seen by the code outputting the framed box. So here's what I got:

    To set the image position and height variables I put this code inside of a while loop, since there are multiple comments in the DB:

    
    
    //Looks at the comment in the DB
    //Calculates the number of lines based on 75 characters per line
    $line_count = ceil(strlen($row3['comment'] /75));
    
    //This sets the position of the username who posted the comment
    $username_pos = ($text_pos + ($line_count * 33)) . ".00pt";	
    
    //Sets the initial starting position of the framed box on the page
    $box_pos = $text_pos - 42.00 . ".00pt";
    
    //Sets the height of the framed box 
    $box_ht = $username_pos + 50 . ".00pt";
    
    //Outputs the framed box 
    echo "<img src='images/blue table.gif' style='position:absolute; left:140.00pt; top:$box_pos'; height:$box_ht'; width = 570.00pt; z-index:-1 class='cc01'>"
    
    
    Code (markup):
    There is also code outputting the comment and username which seems to work just fine.

    The result is a pretty box framing the comment, but the outputted image always ends up with the same height, which obviously doesn't work because each comment in the DB can have a different number of lines and would need a different size box.

    Just to be clear, the source image is much larger than what can be produced by this code.

    If anyone has any suggestions, I'd really appreciate it. Thanks!
     
    John H, Aug 20, 2010 IP
  2. Kudo

    Kudo Peon

    Messages:
    933
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Is "$text_pos" defined before the code you posted??
     
    Kudo, Aug 23, 2010 IP
  3. John H

    John H Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Good question. I should have included that code. It is defined earlier in the script
     
    John H, Aug 23, 2010 IP
  4. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #4
    Your HTML output has extra single quotes that you do not need (also missing one):

    
    echo "<img src='images/blue table.gif' style='position:absolute; left:140.00pt; top:$box_pos'; height:$box_ht'; width = 570.00pt; z-index:-1 class='cc01'>"
    
    PHP:
    Should be:

    
    echo "<img src='images/blue table.gif' style='position:absolute; left:140.00pt; top:$box_pos; height:$box_ht; width = 570.00pt; z-index:-1' class='cc01'>"
    
    PHP:
    As this might not pose a problem in some browsers, others, like Firefox will have problems with it.
     
    ThePHPMaster, Aug 23, 2010 IP
  5. John H

    John H Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for taking the time to analyze my code. I tried it, and for some reason it output the boxes incredibly wide and also, on the last one, incredibly tall. I think it may have something to do with my css sheet perhaps??

    In any event, I played with it for a little while and found a solution that worked. Going through it piece by piece, I discovered that one of the variables wasn't outputting the results I expected. The section of code

    
    
    $line_count = ceil(strlen($row3['comment'] /75));
    
    
    Code (markup):
    always resulted with a value of 1, regardless of the length of the comment. To fix that, I broke it up into two lines

    
    
    $char_count = strlen($row3['comment']);
    $line_count = ceil($char_count / 70);
    
    
    Code (markup):
    That fixed the variable. Then to output the boxes around the comments I made it into two images. First I created a larger box with a border outside of and after the while loop that was not altogether that different from what I had before

    
    
    echo '<img src="images/blue table.gif" style="position:absolute; left:140.00pt; top:373.00pt; height:',$username_pos - 348 . ".00pt",'; z-index:-4"; width = 757.00pt; class="cc01">';	
    
    
    Code (markup):
    and then, inside of the while l inserted a new image, which was essentially a line under each comment

    
    
    //Echos line between comments
    echo '<img src="images/comment.line.gif" style="position:absolute; left:140.00pt; top:',$username_pos + 20 . ".00pt",'"; height:5.00pt; width = 759.00pt>';	
    
    
    Code (markup):
    ---and that got me the results that I needed, though it was a little rough. Thanks for your help!
     
    John H, Aug 24, 2010 IP