MariaL
MariaL

Reputation: 1242

Php and Html code within echo

I'm writing an if statement in which a button needs to show if the cart is empty. For this button I need to get the form key of the product for the data-url So something like this:

<a href="#" data-url="checkout/cart/add/product/59/form_key/<?php echo Mage::getSingleton('core/session')->getFormKey(); ?>/" class="btn btn-success">Order</a>

As mentioned above I need to wrap this button in an if statement, so something like this:

 <?php
    $_helper = Mage::helper('checkout/cart');
    if (1 > $_helper->getItemsCount()){
        echo '<a href="#" data-url="checkout/cart/add/product/59/form_key/<?php echo Mage::getSingleton(\'core/session\')->getFormKey(); ?>/" class="btn btn-success">Order</a>';
    }
    else{
        '<p>hello</p>';
    }
 ?>

But obviously I can't have php echo within echo. Can anybody point me in the right direction of how to do this?

Upvotes: 0

Views: 170

Answers (3)

David
David

Reputation: 218798

You don't put PHP inside HTML inside PHP. Since you're already in the context of PHP code, just concatenate the values you want to the output:

echo '<a href="#" data-url="checkout/cart/add/product/59/form_key/' . Mage::getSingleton('core/session')->getFormKey() . '" class="btn btn-success">Order</a>';

The resulting output is always just a string. You can simply build that string with whatever values you have.

Upvotes: 4

user123
user123

Reputation: 9071

You can just use string concatenation:

echo '<a href="#" data-url=".../' . Mage::getSingleton(...) . '"' ...

Upvotes: 1

Twinfriends
Twinfriends

Reputation: 1987

Simply don't open PHP up again. You can terminate the HTML interpretation inside an echo.

Your code should look like this:

<?php
    $_helper = Mage::helper('checkout/cart');
    if (1 > $_helper->getItemsCount()) {
    echo '<a href="#" data-url="checkout/cart/add/product/59/form_key/'.Mage::getSingleton(\'core/session\')->getFormKey().'/" class="btn btn-success">Order</a>';
    }
    else {
        '<p>hello</p>';
    }
    ?>

Upvotes: 0

Related Questions