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:
JavaScript
x
62
62
1
<?php
2
session_start();
3
if (!isset($_SESSION['admin'])) {
4
header("location: login.php");
5
die();
6
}
7
8
require "conn.php";
9
?>
10
11
<html>
12
<head>
13
<meta charset="UTF-8" />
14
<title>Rückgabe: Admin</title>
15
<link href="rueckgabe-admin.css" rel="stylesheet" />
16
</head>
17
<body style="margin: 50px;">
18
<h1>Rückgaben</h1>
19
<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>
20
<br>
21
<table class="table">
22
<thead>
23
<tr>
24
<th>MYSQL-ID</th>
25
<th>Hardware-ID</th>
26
<th>Lehrkraft</th>
27
<th>Beschädigt?</th>
28
<th>Rückgabedatum</th>
29
<th>Aktion</th>
30
</tr>
31
</thead>
32
33
<tbody>
34
<?php
35
//Rückgabe auslesen
36
$sql = "SELECT * FROM rueckgabe";
37
$result = $conn->query($sql);
38
39
if (!$result) {
40
die("Falsche Anfrage: " . $conn->error);
41
}
42
43
//Alle Zeilen und Spalten lesen
44
while($row = $result->fetch_assoc()) {
45
echo "<tr>
46
<td>" . $row["id"] . "</td>
47
<td>" . $row["rueck_id"] . "</td>
48
<td>" . $row["rueck_lehrkraft"] . "</td>
49
<td>" . $row["rueck_damage"] . "</td>
50
<td>" . $row["rueck_datum"] . "</td>
51
<td>
52
<a class='btn btn-danger btn-sm' href='rueckgabe-delete.php?id=$row[id]'>Löschen</a>
53
</td>
54
</tr>";
55
}
56
57
?>
58
</tbody>
59
</table>
60
</body>
61
</html>
62
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 ?
JavaScript
1
19
19
1
while ($row = $result->fetch_assoc()) {
2
//check if 1 or 0 and set new variable
3
if ($row["rueck_damage"] == "1") {
4
$rueck_damage = "working";
5
} else {
6
$rueck_damage = "broken";
7
}
8
echo "<tr>
9
<td>" . $row["id"] . "</td>
10
<td>" . $row["rueck_id"] . "</td>
11
<td>" . $row["rueck_lehrkraft"] . "</td>
12
<td>" . $rueck_damage . "</td>
13
<td>" . $row["rueck_datum"] . "</td>
14
<td>
15
<a class='btn btn-danger btn-sm' href='rueckgabe-delete.php?id=$row[id]'>Löschen</a>
16
</td>
17
</tr>";
18
}
19