Reputation: 89
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?
Upvotes: 1
Views: 131
Reputation: 1957
You can basically do it in two ways. First:
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; ?>
Upvotes: 1
Reputation: 8643
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>";
}
?>
Upvotes: 3