Skip to content
Advertisement

How can I replace boolean values in a table, imported from a MYSQL database, with a custom string?

I have a php script for an admin panel that shows if a device is broken or not. But it (of course) only displays 1’s and 0’s. I want to make it more user friendly by replacing a 0 with “broken” and a 1 with “working”.

I didn’t try anything yet because I don’t got a single clue on how to “fix” my problem. Here is the code of my admin panel:

    <?php
    session_start();
    if (!isset($_SESSION['admin'])) {
        header("location: login.php");
        die();
    }
    
    require "conn.php";
    ?>
    
    <html>
        <head>
            <meta charset="UTF-8" />
            <title>Rückgabe: Admin</title>
            <link href="rueckgabe-admin.css" rel="stylesheet" />
        </head>
        <body style="margin: 50px;">
            <h1>Rückgaben</h1>
            <a class="btn btn-primary btn-sm" href="ausleihe-admin.php" role="button">Ausleihen</a><a class="btn btn-primary btn-sm" href="ctouch-admin.php" role="button">CTOUCH</a><a class="btn btn-primary btn-sm" href="id-admin.php" role="button">ID</a><a class="btn btn-danger btn-sm" href="logout.php" role="button">Abmelden</a>
            <br>
            <table class="table">
                <thead>
                    <tr>
                        <th>MYSQL-ID</th>
                        <th>Hardware-ID</th>
                        <th>Lehrkraft</th>
                        <th>Beschädigt?</th>
                        <th>Rückgabedatum</th>
                        <th>Aktion</th>
                    </tr>
                </thead>
                
                <tbody>
                    <?php
                    //Rückgabe auslesen
                    $sql = "SELECT * FROM rueckgabe";
                    $result = $conn->query($sql);
                    
                    if (!$result) {
                        die("Falsche Anfrage: " . $conn->error);
                    }
                    
                    //Alle Zeilen und Spalten lesen
                    while($row = $result->fetch_assoc()) {
                        echo "<tr>
                            <td>" . $row["id"] . "</td>
                            <td>" . $row["rueck_id"] . "</td>
                            <td>" . $row["rueck_lehrkraft"] . "</td>
                            <td>" . $row["rueck_damage"] . "</td>
                            <td>" . $row["rueck_datum"] . "</td>
                            <td>
                                <a class='btn btn-danger btn-sm' href='rueckgabe-delete.php?id=$row[id]'>Löschen</a>
                            </td>
                        </tr>";
                    }
                    
                    ?>
                </tbody>
            </table>
        </body>
    </html>

The header “Beschädigt?” is containing the boolean “rueck_damage” from the MYSQL database, which currently only shows 1’s and 0’s. How do I convert those 1’s and 0’s to “working” or “broken” in my table? I want the data in my database to stay a boolean. Thanks for your help!

Advertisement

Answer

if i understood you correct , if the value is 1 show working or 0 then broken ?

while ($row = $result->fetch_assoc()) {
//check if 1 or 0 and set new variable
    if ($row["rueck_damage"] == "1") {
        $rueck_damage = "working";
    } else {
        $rueck_damage = "broken";
    }
    echo "<tr>
              <td>" . $row["id"] . "</td>
              <td>" . $row["rueck_id"] . "</td>
              <td>" . $row["rueck_lehrkraft"] . "</td>
              <td>" . $rueck_damage . "</td>
              <td>" . $row["rueck_datum"] . "</td>
              <td>
                   <a class='btn btn-danger btn-sm' href='rueckgabe-delete.php?id=$row[id]'>Löschen</a>
              </td>
          </tr>";
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement