douyun6399 2017-10-26 14:55
浏览 29
已采纳

php中的HTML链接?

How can I put a html button link in my PHP IF function?

It looks like this:

<?php
    if ( $_SESSION['user_id'] ==  $product["user_id"]){
        <a href="<?php echo base_url() ?>/KdGwController/details_bewerken/<?php echo $product->product_id; ?>"><input type="button" class="btn btn-primary" value="Cadeau bewerken"></input> </a>
    }
?>

But obviously, it won't work because I have html code in it. What is another way to do it?

  • 写回答

2条回答 默认 最新

  • drjgk44268 2017-10-26 15:03
    关注

    You can basically do it in two ways. First:

    UPDATE:

    The comment made by @Vickel is completely right! So I'm editing your html to what it should be.

    Echoing a string:

    <?php
        if ( $_SESSION['user_id'] ==  $product["user_id"]){
            // Wrong html
            // echo '<a href="'. base_url() . '/KdGwController/details_bewerken/' . $product->product_id . '"><input type="button" class="btn btn-primary" value="Cadeau bewerken"></input> </a>';
            // Right html
            echo '<a class="btn btn-primary" href="'. base_url() . '/KdGwController/details_bewerken/' . $product->product_id . '"> Cadeau bewerken </a>';
        }  
    ?>
    

    Second. Closing the PHP:

    <?php
        if ( $_SESSION['user_id'] ==  $product["user_id"]){
    ?>
            <!-- Wrong html -->
            <!-- <a href="<?php echo base_url() ?>/KdGwController/details_bewerken/<?php echo $product->product_id; ?>"><input type="button" class="btn btn-primary" value="Cadeau bewerken"></input> </a> -->
            <!-- Right html -->
            <a class="btn btn-primary" href="<?php echo base_url() ?>/KdGwController/details_bewerken/<?php echo $product->product_id; ?>"> Cadeau bewerken </a>
    
    <?php
            }  
    ?>
    

    I personally prefer the second because of the syntax highlight. An ever better way of doing this would be using the "syntax sugar if":

    <?php if ( $_SESSION['user_id'] ==  $product["user_id"]): ?>
        <!-- Wrong html -->
        <!-- <a href="<?php echo base_url() ?>/KdGwController/details_bewerken/<?php echo $product->product_id; ?>"><input type="button" class="btn btn-primary" value="Cadeau bewerken"></input> </a> -->
        <!-- Right html -->
        <a class="btn btn-primary" href="<?php echo base_url() ?>/KdGwController/details_bewerken/<?php echo $product->product_id; ?>"> Cadeau bewerken </a>
    <?php endif; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度