treehouse : what would you like to learn today?
Web Design Web Development iOS Development

How to write a simple HTML code inside PHP echo " ";

  •   <IMG SRC="images/linkedin-icon.png" ALT=" " 
        ONMOUSEdown="document.images['large'].src='images/linkedin.png'">
      <IMG SRC="images/facebook-icon.png" ALT=" " 
        ONMOUSEdown="document.images['large'].src='images/facebook.png'">
      <BR><BR>
      <IMG SRC=" " ALT=" " NAME="large">
    

    How do I write this code inside PHP echo " ";

      <?php
      echo "
    
        <img src='images/linkedin-icon.png' onMouseDown='document.images[\'large\'].src=\'images/linkedin.png\' ' />
                          <img src='images/facebook-icon.png' onMouseDown='document.images[\'large\'].src=\'images/facebook.png\' ' />
        <img src=' ' name='large'>
    
      " 
      ?>  
    

    I tried writing it just like the above code but it doesn't work.

  • Need to add a ; to the end of your echo after the closing quote mark.

  • just so you know, this:

    <?php
      echo "
    
        <.img src='images/linkedin-icon.png' onMouseDown='document.images[\'large\'].src=\'images/linkedin.png\' ' />
        <.img src='images/facebook-icon.png' onMouseDown='document.images[\'large\'].src=\'images/facebook.png\' ' /> 
        <.img src=' ' name='large'>
        
          " ;
    
    
    and this:
    
    
        <?php
        ?>
      <.img src='images/linkedin-icon.png' onMouseDown='document.images[\'large\'].src=\'images/linkedin.png\' ' />
       <.img src='images/facebook-icon.png' onMouseDown='document.images[\'large\'].src=\'images/facebook.png\' ' />
      <.img src=' ' name='large'>
    

    are exactly equivalent.

    Also, when writing HTML (or anything else that may have quote marks), it's usually much easier to use the HEREDOC notation:

      $string = <<< HTML
          Hello, I'm using "quotes" left and right, and I don't need to escape them.
          I can still use $variables in my text, 
          or even {$nested['array']['indexes']}.
          A "HEREDOC" is like a "double-quote" string, 
          but it starts with three less-than brackets ( <<< )
          followed by a token (anything; I used "HTML" here)
          and ends when the token appears at the beginning of a line
          (by itself, no indentation at all),
          with a semicolon by itself on the next line.
          Have a try!
      HTML
      ;
    
  • Try this:

    <?php if ( is_page_template('template-fullwidth.php') ) { ?>
        your html here
    <?php } elseif ( is_page_template('template-planes.php') ) { ?>
      your html here
    <?php } else { ?>
             your html here
    <?php } ?>
    
  • Thank you for all your answers ( @Rai, @traq, @AndyHowells ). One problem that I keep on encountering with that code is the pure html works and does swap image but when I try to incorporate it inside PHP, it doesn't work anymore. That is why i'm looking for a work around on how to write it inside PHP because I will be getting the image source from a database if only those code will work. :( Please help...

  • Well, what do you mean by "doesn't work"?

    What actually happens vs. what you expected?

    Did you make sure error reporting is enabled, and do you get any error messages?

  • @traq, Sorry if I sound a little confusing. The pure HTML markup with a little javascript I posted does a "swap image". The small/thumbnail images when clicked , a bigger version of that image will appear below. But then when I try to rewrite the code inside PHP and run it in a browser, it doesn't do anything when you click the thumbnail image but I didn't get any error messages.

  • @ajnoguerra what does the output HTML look like?

  • @traq, I'm glad you're really trying to help me. Here's the output >>> swap image

  • Here's the output

    That seems to work as expected, unless I misunderstand your problem.

    Are you sure you posted the output HTML (i.e., the HTML that is generated by PHP and demonstrates your problem)?

  • <?php
      mysql_connect("localhost","root","") or die (mysql_error());
      mysql_select_db("motoactiv-db") or die (mysql_error());
      
      $sql = "Select * FROM products";
      $result = mysql_query($sql);
      while($record=mysql_fetch_array($result))
      {
      ?>
    
    <img src="images/helmets/<?php echo $record['side_view']?>"  width="100" 
    onmousedown="document.images['main'].src='images/helmets/<?php echo $record['top_view']?>'">
    </br>
    <img name="main">
    
    
    <?php
      }
      ?>
    

    I still haven't figured out the problem with this because when I tested it out in Firefox and IE it works just fine but when I tried it with Chrome, it doesn't make the bigger image come out, it is as if no javascript was implemented. @traq

  • @ajnoguerra - see my comment in your other thread.

  • image

    Why are you escaping the quotes? You don't need to do that. You only need to do that when the quotes are the same as the surrounding quotes. If your html is inside double quotes, you don't need to escape single quotes.

  • <?php
      mysql_connect("localhost","root","") or die (mysql_error());
      mysql_select_db("motoactiv-db") or die (mysql_error());
      
      $sql = "Select * FROM products";
      $result = mysql_query($sql);
      while($record=mysql_fetch_array($result)) : ?>
    
      <img src="images/helmets/<?php echo $record['side_view']?>"  width="100" 
      onmousedown="document.images['main'].src='images/helmets/<?php echo $record['top_view']?>'" />
      <br />
      <img name="main" />
    
      <?php endwhile ?>
    

    I've just taken your code and modified it slightly. You hadn't ended your <img> tags, that could've been the source of your problem. I would also move the JS to a separate file and call it by ID.

    The PHP syntax used is the alternate form which makes it slightly easier to read.