Rollover Image inside php - Need help

Discussion in 'PHP' started by Huo_Yang, Feb 2, 2009.

  1. #1
    Hi.

    I am trying to get an rollover image (code from dreamweaver) to work inside of php code.

    I am having troubles with ' or "

    }      
              echo '<a href="http://example-url.com/index.php?task=logout" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('btnlogout','','http://example-url.com/template/images/button_logout_roll.png',1)"><img src="http://example-url.com/template/images/button_logout.png" name="btnlogout" width="88" height="30" border="0" id="btnlogout" /></a>';
              
          }
    else
          {
    
    PHP:
    this is what I get:
    Parse error: syntax error, unexpected T_STRING, expecting ',' or ';'


    I have no clue how to get this working.
    thnx in advance.
     
    Huo_Yang, Feb 2, 2009 IP
  2. Dangy

    Dangy Well-Known Member

    Messages:
    841
    Likes Received:
    25
    Best Answers:
    2
    Trophy Points:
    155
    #2
    You don't have to use the echo tag here just use my code.


    
    }     ?>
              <a href="http://example-url.com/index.php?task=logout" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('btnlogout','','http://example-url.com/template/images/button_logout_roll.png',1)"><img src="http://example-url.com/template/images/button_logout.png" name="btnlogout" width="88" height="30" border="0" id="btnlogout" /></a>
             
         <? }
    else
          {
    
    PHP:
     
    Dangy, Feb 2, 2009 IP
  3. Huo_Yang

    Huo_Yang Peon

    Messages:
    250
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    after the
    }
    else
          {
    PHP:
    comes a different rollover image, and with your code it seems that the "if" "else" does get cutt off, so it shows me both rollover images, instead of just one of them.

    here is the full code
     
    Huo_Yang, Feb 2, 2009 IP
  4. ez-designs

    ez-designs Well-Known Member

    Messages:
    230
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #4
    Hey if you are using quotes ("") around your strings, and you want to use quotes ("") inside those strings, but don't want them to be part of the code to be executed, you can use the backslash / to escape them. The same is true if you are using apostrophes '.
     
    ez-designs, Feb 2, 2009 IP
  5. Huo_Yang

    Huo_Yang Peon

    Messages:
    250
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Still no clue, of how to fix this. Tried it for hours now.
     
    Huo_Yang, Feb 2, 2009 IP
  6. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #6
    this is your problem line:

    echo '<a href="http://example-url.com/index.php?task=logout" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('btnlogout','','http://example-url.com/template/images/button_logout_roll.png',1)"><img src="http://example-url.com/template/images/button_logout.png" name="btnlogout" width="88" height="30" border="0" id="btnlogout" /></a>';

    you encapsulate the echoed string in single quotes. then later on where you define the inline javascript, you have other instances of single quotes, like: 'btnlogout','','

    these need to be escaped by adding a slash like so: \'btnlogout\',\'\',\'... etc etc, otherwise PHP compiler registers the end of your string and tries to eval the next bit, which happens to be 'btnlogout'.

    i hope this explains why you get the error and helps you understand how to avoid repeating it :)
     
    dimitar christoff, Feb 2, 2009 IP
  7. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #7
    Just like dimitar christoff said..

    An easier way to show it:
    You got this code:
    
    <?
    echo "Hello "world". This is a test.";
    ?>
    PHP:
    You can see.. Your echo starts with " and around the word world, we have " and ". Those will break your echo, because the compiler thinks it ends at the first " (echo "hello world";). But since it cannot find ; after the echo, it will return false.

    So you have to escape the string by putting \ in front of any ". So:
    <?
    echo "Hello \"world\". This is a test";
    ?>
    PHP:
    If you use ' instead of ", you'll have to use backslash in front of every ', but not ":
    <?
    echo 'Hello "world". This is a test.'; //This one will work.
    echo 'Hello 'world'. This is a test.'; //This one will NOT work.
    echo "Hello 'world'. This is a test.'; //This one will work.
    echo "Hello "world". This is a test."; //This one will NOT work.
    
    //Escaped:
    echo "Hello \"world\". This is a test"; //This one will work
    echo 'Hello \'world\'. This is a test"; //This one works too
    ?>
    PHP:
     
    elias_sorensen, Feb 3, 2009 IP