#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    kerala, India
    Posts
    96
    Rep Power
    7

    Adding HTML in PHP


    Hi Friends,

    How to add HTML in PHP?
    Below code is not working. Could you please help me?

    PHP Code:
    <?php 
                      $HTML
    ='<tr>
                      <td><?php echo $i++;?></td>
                      <td><?php echo $customerBookingArray["cus_contactName"];?></td>
                      <td><?php echo $customerBookingArray["cus_contactNo"];?></td>
                      <th><?php echo $customerBookingArray["cus_email"];?></th> 
                      <tr>'
    ;
                      
    ?>
    Thanks

    Sarath.
    Last edited by ManiacDan; May 20th, 2014 at 08:07 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,082
    Rep Power
    381
    look at what you are doing..

    hint look at <?php and '
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2014
    Location
    Australia
    Posts
    9
    Rep Power
    0
    Basically there are 2 ways

    1st way

    PHP Code:
     html '<p> I can store html in a var and echo out the var</p>;
    echo $ html 
    Or close your php off and write in html
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,884
    Rep Power
    6356
    First of all, in the future you need to define "not working." Technically this code works fine, you don't understand what it does. "Not working" is a phrase that can mean anything from "$i isn't properly incremented" to "my computer turns off whenever I run this." Describe the problem in terms of the difference between what you SEE and what you EXPECTED.

    Now for your actual problem: You can't open PHP tags when you're already inside PHP and you can't do variable interpolation within single-quoted strings. The more correct way of doing this would be:


    PHP Code:
    <?php 
                      $HTML
    ='<tr>
                      <td>' 
    strval($i++) . '</td>
                      <td>' 
    $customerBookingArray["cus_contactName"] . '</td>
                      <td>' 
    $customerBookingArray["cus_contactNo"] . '</td>
                      <th>' 
    $customerBookingArray["cus_email"] . '</th> 
                      <tr>'
    ;
                      
    ?>
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0
    You first know about concatenation in PHP. Try this
    <?php
    $HTML='<tr>
    <td>' . strval($i++) . '</td>
    <td>' . $customerBookingArray["cus_contactName"] . '</td>
    <td>' . $customerBookingArray["cus_contactNo"] . '</td>
    <th>' . $customerBookingArray["cus_email"] . '</th>
    <tr>';
    ?>

    Comments on this post

    • ManiacDan disagrees : Begone foul spammer
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    51
    Rep Power
    48
    If im not mistaken you can also use :

    PHP Code:
     <?php 
                      $HTML
    ='<tr>
                      <td>{strval($i++) }</td>
                      <td>{$customerBookingArray["cus_contactName"]}</td>
                      <td>{$customerBookingArray["cus_contactNo"]}</td>
                      <th>{$customerBookingArray["cus_email"]}</th> 
                      <tr>'
    ;
     
    ?>
  12. #7
  13. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,884
    Rep Power
    6356
    You are mistaken, single-quoted strings cannot contain variables since the interpolation won't work. You could do this:
    PHP Code:
     <?php  
                      $HTML
    ="<tr> 
                      <td>" 
    strval($i++) . "</td> 
                      <td>
    {$customerBookingArray['cus_contactName']}</td> 
                      <td>
    {$customerBookingArray['cus_contactNo']}</td> 
                      <th>
    {$customerBookingArray['cus_email']}</th>  
                      <tr>"

     
    ?>
    Note I also had to break the string for the strval() call. ++ cannot be called within string interpolation. Either this method or the one I posted last time will work. This one is very slightly faster, and is the one recommended for PHP because of readability (now that it's highlighted properly with the correct quotes you can see why)
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo