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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?