I am new and learning PHP and HTML. I have one user table where I am displaying data. I want to show Delete button if user_id = 1. else I want to hide it. My current link code is like below
<a href="<?php echo site_url('members/delete/'.$row['user_id']); ?>" class="btn btn-danger" onclick="return confirm('Are you sure to delete?')">delete</a>
I have done PHP code for doing achieve my above description
<?php
if ($row['user_id']==1){
echo //// I want a link here
}
?>
But since my link have also used some PHP codes, I am confused and not able to properly echo button. I am trying from an hour and not able to fix it. Let me know if someone can help me for do it.
Thanks!
您可以像这样将 html 代码放在 echo 中
echo "<tag name></tag name>"
Read about string concatenation in PHP.
Here is a possible solution for your problem:
<?php
if ($row['user_id'] === 1) {
echo '<a href="/members/delete/'.$row['user_id'].'">'.$row['username'].'</a>';
}
I think you want to add variables in string:
<?php
if ($row['user_id']==1){
echo '<a href="'.site_url('members/delete/'.$row['user_id']).'" class="btn btn-danger" onclick="return confirm(\'Are you sure to delete?\')">delete</a>';
}
?>
Use .
to combine strings & variables:
'string'.$var
here an an example...
<?php if ($row['user_id']==1){ ?>
<a href="<?php echo site_url('members/delete/'.$row['user_id']); ?>" class="btn btn-danger" onclick="return confirm('Are you sure to delete?')">delete</a>
<?php } ?>
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.