Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Nov 2006
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts

    html code within php code?

    How can I insert HTML code from within a php statement?

    eg

    if validation
    {
    html code snippet to display message
    }else{
    HTML Code snippet to display error page
    }
    }

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,746
    Thanks
    18
    Thanked 155 Times in 146 Posts
    PHP Code:
     if (validation)
     {
       echo 
    '<p>Lookin\' good.</p>';
     }
    else
    {
        echo 
    '<p>There was an error.</p>';
     } 
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #3
    Regular Coder
    Join Date
    Nov 2006
    Posts
    106
    Thanks
    0
    Thanked 0 Times in 0 Posts
    what if I wanted in include a large ammount of html instead of just a single line that also included inline php?

  • #4
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,746
    Thanks
    18
    Thanked 155 Times in 146 Posts
    what if I wanted in include a large ammount of html instead of just a single line that also included inline php?

    Here's a chunk of some of my own code to give you an idea of how that would look:

    PHP Code:
    $the_body .= '<div id="itemized">

                    <div id="item_left">'
    ;

                        foreach (
    $item_array as $key => $val)
                        {
                            
    $the_body .= '<p class="line2">'.$item_array[$key]['quantity'].' '.$item_array[$key]['description'].'</p>';
                        }

                        
    $the_body .= '<p class="line"> </p>
                        <p class="line2">Subtotal</p>
                        <p class="line2">Sales Tax</p>'
    ;

                        if (
    $pickup == "NO")
                        {
                            
    $the_body .= '<p class="line2">Shipping Cost';

                            if (!empty(
    $shipping_comp))
                            {
                                
    $the_body .= ' <span style=\'color:red\'>('.$shipping_comp.')</span>';
                            }

                            
    $the_body .= '</p>';
                        }

                        
    $the_body .= '<p class="line2">Handling Cost</p>
                        <p class="line"> </p>
                        <p class="line2">Total</p>

                    </div>

                    <div id="item_right">'
    ;

                        
    $subtotal 0;
                        foreach (
    $item_array as $key => $val)
                        {
                            
    settype($item_array[$key]['price'],"float");
                            
    settype($item_array[$key]['quantity'],"integer");

                            
    $the_body .= '<p class="line2">$'.number_format($item_array[$key]['price'],2).' each</p>';

                            
    $subtotal += ($item_array[$key]['price'] * $item_array[$key]['quantity']);
                        }

                        
    $tax round($subtotal 0.072);
                        
    settype($shipping_cost,"float");

                        
    $target_earnings $subtotal $tax $shipping_cost;

                        if (
    $country == "United States")
                        {
                            
    $total_charge round(($target_earnings 0.30)/0.9712);
                        }
                        else
                        {
                            
    $total_charge round(($target_earnings 0.30)/0.9612);
                        }

                        
    $handling_cost $total_charge $target_earnings;

                        
    $the_body .= '<p class="line"> </p>';
                        
    $the_body .= '<p class="line2">$'.number_format($subtotal,2).'</p>';
                        
    $the_body .= '<p class="line2">$'.number_format($tax,2).'</p>';

                        if (
    $pickup == "NO")
                        {
                            
    $the_body .= '<p class="line2">$'.number_format($shipping_cost,2).'</p>';
                        }

                        
    $the_body .= '<p class="line2">$'.number_format($handling_cost,2).'</p>';
                        
    $the_body .= '<p class="line"> </p>';
                        
    $the_body .= '<p class="line2">$'.number_format($total_charge,2).'</p>';

                    
    $the_body .= '</div>

                    <div style="clear:both;height:45px"> </div>

                </div>'



    If you replace every instance of
    "$the_body .=" with "echo", you should get the idea...
    Last edited by chump2877; 11-17-2006 at 10:29 PM.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #5
    Senior Coder
    Join Date
    Jul 2005
    Location
    UK
    Posts
    1,051
    Thanks
    6
    Thanked 13 Times in 13 Posts
    If HTML is in abundance and you only need to include the occasional bit of PHP, an easier way is to break in and out of PHP as required.

    PHP Code:
    <?php if validation ?>

    html code snippet <?php inline ?> to display message

    <?php  } else { ?>

    HTML Code snippet to display error page

    <?php ?>
    Last edited by Pennimus; 11-17-2006 at 10:41 PM.

  • #6
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,746
    Thanks
    18
    Thanked 155 Times in 146 Posts
    Quote Originally Posted by Pennimus View Post
    If HTML is in abundance and you only need to include the occasional bit of PHP, an easier way is to break in and out of PHP as required.

    PHP Code:
    <?php if validation ?>

    html code snippet <?php inline ?> to display message

    <?php  } else { ?>

    HTML Code snippet to display error page

    <?php ?>
    I suppose that makes it easier to read, but it also depends on how much "inline PHP" you have...if you have large chunks of HTML code only, than using <? ?> makes more sense than echoing out everything. If you find that you have a lot of "inline PHP", then either way can work, depending on your preference.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •