laurence keith albano
laurence keith albano

Reputation: 1482

How to add specific edit button on table for certain condition?

I want to display the Edit button in my data only if the status stated in my database is 'Full';

In my code below I added an edit button to each status dynamically. What I want to achieve is to only add the Edit button to the status that has a value of Full only. Let's say I have two data in my database, one Status says Full and the other one says Available. How do I add the Edit button to the status who has the Full data only. Is this possible? Please help.

I've tried this one using echo, but it seems it is an incorrect syntax.

<?php 
echo "" ."<?php if($row['status'] == Full) { <a class='editname' href='editnames.php?did=".$row['nameid']."'>Edit</a> } ?>". "";
?>

Is this doable in php code?

<?php
include "includes/connection.php";
session_start(); // start a session

// query from db
$sql = $connection->prepare('SELECT nameid, name2, name3, name4, status FROM names');
$sql->execute(); // execute query
$result = $sql->get_result(); // fetch result

if ($result->num_rows > 0) {
    echo "<tr><th>Name 1</span></th><th>Name 2</th><th>Name 3</th><th>Name 4</th><th>Status</th><th></th></tr>";
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<tr><td>" .$row["nameid"]. "</td><td>" .$row["name2"]. "</td><td>" .$row["name3"]. ", " .$row["name4"]. "</td><td>" .$row["status"]. "</p></td><td>" ."<a class='editname' href='editnames.php?did=".$row['nameid']."'>Edit</a>". "</td></tr>";
    }
} else {
    echo "0 results";
}

?>

Upvotes: 0

Views: 433

Answers (1)

Hasta Dhana
Hasta Dhana

Reputation: 4719

You could make the conditional status within the while loop like this :

while($row = $result->fetch_assoc()) {
    $status = $row["status"] == 'Full' ? "<a class='editname' href='editnames.php?did=".$row['nameid']."'>Edit</a>" : "";
    echo "<tr><td>" .$row["nameid"]. "</td><td>" .$row["name2"]. "</td><td>" .$row["name3"]. ", " .$row["name4"]. "</td><td>" .$row["status"]. "</p></td><td>" . $status . "</td></tr>";
}

Upvotes: 1

Related Questions