...

View Full Version : html code within php code?



jedimastermopar
11-17-2006, 08:58 PM
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
}
}

chump2877
11-17-2006, 09:03 PM
if (validation)
{
echo '<p>Lookin\' good.</p>';
}
else
{
echo '<p>There was an error.</p>';
}

jedimastermopar
11-17-2006, 10:53 PM
what if I wanted in include a large ammount of html instead of just a single line that also included inline php?

chump2877
11-17-2006, 11:26 PM
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:


$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.07, 2);
settype($shipping_cost,"float");

$target_earnings = $subtotal + $tax + $shipping_cost;

if ($country == "United States")
{
$total_charge = round(($target_earnings + 0.30)/0.971, 2);
}
else
{
$total_charge = round(($target_earnings + 0.30)/0.961, 2);
}

$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...

Pennimus
11-17-2006, 11:34 PM
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 if validation { ?>

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

<?php } else { ?>

HTML Code snippet to display error page

<?php } ?>

chump2877
11-17-2006, 11:45 PM
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 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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum