fantasiaa123
fantasiaa123

Reputation: 25

php mysql show table badge based on what result is

I am using php to get data from my database and displaying the rows into a bootstrap table. How would I make it do the following; if $res['status'] (status) is equal to "example 1" then a primary bootstrap badge will show in the status column with the text "example 1", if its "example 2" then warning-badge and "example 3", danger-badge

enter image description here

echo "<table class='table'><tr>";
                        echo "<th>Signup Date</th>";
                        echo "<th>Company Name</th>";
                        echo "<th>Status</th>";



         while($res = mysqli_fetch_array($query)){

            $signd = $res['signupDate'];
            $cname = $res['CompanyName'];
            $stat = $res['status'];


             echo "<tr><td><p>$signd</p></td>"; 
             echo "<td><p>$cname</p></td>";
            echo "<td><p>$stat</p></td></tr>";

         }

echo "</table>";

?>

Upvotes: 0

Views: 583

Answers (1)

clash
clash

Reputation: 427

There are a couple of ways to achieve that. One way would be a simple switch statement that checks the value of $stat and sets the bootstrap class in another variable. You then can just add the css-class variable to the html-tag.

<?php
while ($res = mysqli_fetch_array($query)) {

    $signd = $res['signupDate'];
    $cname = $res['CompanyName'];
    $stat = $res['status'];
    $class = '';

    switch($stat) {
        case "example 1":
            $class = 'primary-badge';
        break;
        case "example 2":
            $class = 'warning-badge';
        break;
        case "example 3":
            $class = 'danger-badge';
        break;
        default:
            $class = '';
    }


    echo "<tr><td><p>$signd</p></td>";
    echo "<td><p>$cname</p></td>";
    echo "<td class=\"$class\"><p>$stat</p></td></tr>";

}

Something like this should work (it may be that the class names are different or that they belong to another html-tag, but you get the idea).

Upvotes: 1

Related Questions