简体   繁体   中英

HTML link in php?

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?

You could do 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>
<?php
    }  
?>

Or alternatively, have PHP echo or print() the html tag. (which is somewhat cleaner, but requires that you escape your " characters)

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

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; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM