How to use if condition in echo

537 Views Asked by At

I have a form to add product to cart and inside it there is a link.

This is link

<?php 
    echo $this->Html->link('<div class="single-products">'.'<div class="productinfo text-center myimg">'.$this->Html->image("product/".$row["Product"]["photo"],array(/*"width"=>"2500px",*/"height"=>"250px")).'<h2> &#8377  '.$row["Product"]["price"].'</h2>'.'<p>'.$row["Product"]["name"]."</p><a href='javascript:document.ff".($i++).".submit()' class='btn btn-default add-to-cart'><i class='fa fa-shopping-cart'></i>Add to cart</a>".'</div>'.'</div>',
        array
        (
            'controller'=>'Public',
            'action'=>'singleproduct?id='.$row["Product"]["id"],
        ),
        array
        (
            'escape'=>false  //NOTICE THIS
        )
    );
?>

And I want to use this code just above Add to cart button

<?php

    if($row["Product"]["psize"]==1)
    {
?>
        Size<select name="psize">
                <option value="S">S</option>
                <option value="M">M</option>
                <option value="L">L</option>
                <option value="XL">XL</option>
            </select>
<?php
    }
    elseif($row["Product"]["psize"]==2)
    {
?>
        Size<select name="psize">
                <option value="28">28</option>
                <option value="30">30</option>
                <option value="32">32</option>
                <option value="34">34</option>
            </select>
<?php
    }

?>

This code is working if I put it outside of this HTML helper link, but because of design problem and I want to display it just above the add to cart button I have tried but couldn't figure out how to put this inside link.

2

There are 2 best solutions below

0
On
                <?php

                if($row["Product"]["psize"]==1)
                {
                ?>
                Size<select name="psize">
                        <option value="S">S</option>
                        <option value="M">M</option>
                        <option value="L">L</option>
                        <option value="XL">XL</option>
                    </select>
                <?php
                } else{

                        if($row["Product"]["psize"]==2){

                            ?>
                        Size<select name="psize">
                        <option value="28">28</option>
                        <option value="30">30</option>
                        <option value="32">32</option>
                        <option value="34">34</option>
                    </select>
                <?php

                        } else{
                        echo "TEY IT"
                        }
                }


                ?>

May Be it gone help.

0
On

Bring out your codes in the function $this->Html->link(), assign it to a variable and use if condition. And i think you should use CakePHP Form Helper to output select box.

Example:

<?php

$select = $this->Form->input('psize', array(type => 'select', 'options' => $sizeOptions)); // you can use if conditions here

$link = $this->Html->link('<div>...</div>' . $select . '<div>...</div>', $yourUrlArr);
echo $link;