0

I would like to place an if statement within an echo and I am not quite sure how to do it. Here is the echo:

if(!$hideProduct) {         
echo '
    <div class="gearBorder">
        <img title="Sold Out" alt="Sold Out" class="soldOut" src="soldout.png" />':"").'
        <div class="gearInfo">
            <h4>' . $productName . '</h4>
            <p class="gearDesc">'. $productDescription .'</p>
            <p class="cost">$' . $productPrice . '</div>
        </div>  
    </div>
';}

On line 3, I would like to wrap the image in an if statement:

if($productStatus = '0') {

}

What would be the best way to wrap the image in that statement? Thanks!

2 Answers 2

1

You can actually end control flow blocks like if statements outside of the same PHP block they were opened in. For example, this should work:

<?php if (!$hideProduct) { ?>
    <div class="gearBorder">

        <?php if ($productStatus == '0') { ?>
            <img title="Sold Out" ... />
        <?php } ?>

        ...HTML...

    </div>
<?php } ?>

If you don't like the curly braces, you can also replace them with a colon (:) and endif, respectively. See this link for more information.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, thank you for the example and the resource!
0

Use an array to hold the CSS classes (with background-image) for each $productStatus
Fast and efficient. There is a performance hot when you toggle from HTML mode to PHP mode. This method eliminates the if elseif performance hit in the Intel micro code.

<style type="text/css">
.soldout{width:40px;height:40px;background-image: url('soldout.png');}
.backorder{width:40px;height:40px;background-image: url('backorder.png');}
.instock{width:40px;height:40px;background-image: url('instock.png');}
</style>

$icon = array(' class="soldout" ',' class="backorder" ',' class="instock" ');.

echo '<div class="gearBorder"><div ' . $icon[$productStatus] . '></div><div class="gearInfo"> ... ';

I would also use a 4-bit color GIF icon and convert it to Base64 MIME
Make sure page is served with gZip and there will be little to no penalty for the Base64.
If you want to stick with image files, make sure images are served with a large cache max-age value.

background-image: url('data:image/gif;base64,R0lGODlhKAAoAK...');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.