Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

If statement inside While loop

I want the delete button to have an If statement inside my While loop so that the current user won’t be able to delete its account but it’s always having an error. What to do? Here’s my if statement btw;

<?php if($_SESSION["username"]!= $array[2]){
echo <a class="btn btn-danger delete_user" href="javascript:void(0)" data-id = '<?php echo $row['id'] ?>'>Delete</a> 
}

and here is my while loop;

<?php while($array=mysqli_fetch_row($result)): ?>
                            <tr>
                                <td>
                                    <?php echo $array[1];?>
                                    <?php echo $array[2];?>
                                </td>
                                
                                <td>
                                    <?php echo $array[3];?>
                                </td>
                                <td>
                                    <center>
                                        <a class="btn btn-primary edit_user" href="javascript:void(0)" data-id = '<?php echo $row['id'] ?>'>Edit</a>
                                        <a class="btn btn-danger delete_user" href="javascript:void(0)" data-id = '<?php echo $row['id'] ?>'>Delete</a>
                                    </center>
                                </td>
                            </tr>
                        <?php endwhile; ?> 

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You didn’t mention the exact error, but I can see a few: String literals must always have quote marks round them, and also you can’t open a PHP block inside another one, or echo inside an echo.

Just close the php block before you start the HTML, and open it again afterwards for the closing bracket:

<?php if($_SESSION["username"]!= $array[2]) { ?>
  <a class="btn btn-danger delete_user" href="javascript:void(0)" data-id = '<?php echo $row['id'] ?>'>Delete</a>  
<?php } ?>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading