cakephp:我怎么能在cakephp中写这个链接

时间:2016-03-01 17:36:11

标签: php cakephp cakephp-2.7

<a href="../Public/singleproduct?id=<?php echo $row["Product"]["id"];?>">
        <div class="single-products">
            <div class="productinfo text-center myimg">
                <?php echo $this->Html->image("product/".$row["Product"]["photo"]); ?>
                <h2>Rs.<?php echo $row["Product"]["price"];?></h2>
                <p><?php echo $row["Product"]["name"];?></p>
                <a href="javascript:document.ff<?php echo ($i++);?>.submit()" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a>
                <?php echo $this->Session->flash('flash', array('element' => 'flash_notification')); ?>
            </div>
        </div>
</a>

在使用分页时,我需要以cakephp的方式转换此链接,因为在其他分页页面上,此html <a href=""></a>将无效。

这是我的.ctp文件的所有代码

<?php
 $i=0;
 foreach($product as $row)
 {
?>
 <form name="ff<?php echo $i;?>" method="post">
    <input type="hidden" name="product_tbls_id" value="<?php echo $row["Product"]["id"];?>">
    <input type="hidden" name="qty" value="1">
    <div class="col-sm-4">
      <div class="product-image-wrapper">
        <a href="../Public/singleproduct?id=<?php echo $row["Product"]["id"];?>">
          <div class="single-products">
             <div class="productinfo text-center myimg">
                <?php echo $this->Html->image("product/".$row["Product"]["photo"]); ?>
                <h2>Rs.<?php echo $row["Product"]["price"];?></h2>
                <p><?php echo $row["Product"]["name"];?></p>
                <a href="javascript:document.ff<?php echo ($i++);?>.submit()" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a>
                <?php echo $this->Session->flash('flash', array('element' => 'flash_notification')); ?>
             </div>
          </div>
        </a>
      </div>
    </div>
</form>
<?php
}
?>

2 个答案:

答案 0 :(得分:0)

你的问题有点令人困惑。 仍然将“公共”称为“控制者”,将“单一产品”视为“行动”,您可以这样写:

<?php echo $this->Html->link('link text here', array(
'controller' => 'Public',
'action' => 'singleproduct',
'?' => array('id' => $row["Product"]["id"]))
); ?>

答案 1 :(得分:0)

我试过这个并且它对我有用。只是因为它可以帮助某人而给出答案。

<?php 
    echo $this->Html->link('<div class="single-products">'.'<div class="productinfo text-center myimg">'.$this->Html->image("product/".$row["Product"]["photo"]).'<h2>'.$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>".$this->Session->flash('flash', array('element' => 'flash_notification')).'</div>'.'</div>',
                                                                                array
                                                                                (
                                                                                    'controller'=>'Public',
                                                                                    'action'=>'singleproduct?id='.$row["Product"]["id"],
                                                                                ),
                                                                                array
                                                                                (
                                                                                    'escape'=>false
                                                                                )
                                                                            );
?>