Home » Php » How to show a link if user_id =1 in HTML with PHP

How to show a link if user_id =1 in HTML with PHP

Posted by: admin February 25, 2020 Leave a comment

Questions:

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!

How to&Answers:

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

Answer:

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>';
}

Answer:

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

Answer:

you can put html code in echo like this

 echo "<tag name></tag name>"