My problem is that the values in the table are colored incorrectly. The mean value should actually be green, because it is higher than the previous value.
Can you please explain to me where the mistake is?
$aktienArray = array();
$sql = "SELECT * FROM Aktien ORDER BY Name ASC";
if ($stmt = $pdo->prepare($sql)) {
$stmt->execute();
while ($a = $stmt->fetch(PDO::FETCH_ASSOC))
$aktienArray[] = $a;
}
foreach ($aktienArray as $aktie) {
$symbol = $aktie['Symbol'];
$name = array();
$sqlName = "SELECT Name FROM Aktien WHERE Symbol=:symbol";
if ($stmtName = $pdo->prepare($sqlName)) {
$stmtName->execute( array(':symbol' => $aktie['Symbol']) );
$n = $stmtName->fetch(PDO::FETCH_ASSOC);
}
echo "<h5>" . $n['Name'] . "</h5>";
echo "<table class=\"table table-dark\">\n\t";
echo "<thead>
<tr>
<th>Symbol</th><th>Datum</th><th>Stand</th>
</tr>
</thead>
<tbody>";
$dArray = array();
$sql3 = "SELECT Stand
FROM Aktienstand
WHERE Symbol=:symbol
AND Datum >= NOW() - INTERVAL 3 DAY";
if ($stmt3 = $pdo->prepare($sql3)) {
$stmt3->execute( array(':symbol' => $symbol) );
while ($date = $stmt3->fetch(PDO::FETCH_ASSOC))
$dArray[] = $date;
$kurse = array();
$sql2 = "SELECT Symbol, Datum, Stand
FROM Aktienstand
WHERE Symbol=:symbol";
if ($stmt2 = $pdo->prepare($sql2)) {
$stmt2->execute( array(':symbol' => $symbol) );
while ($k = $stmt2->fetch(PDO::FETCH_ASSOC)) $kurse[] = $k;
foreach ($kurse as $kurs) {
if ($kurs['Stand'] > $dArray['0']['Stand']) {
echo "<tr>\n";
echo "<td id='positive'>";
echo($kurs['Symbol']);
echo "</td>\n";
echo "<td id='positive'>";
echo($kurs['Datum']);
echo "</td>\n";
echo "<td id='positive'>";
echo($kurs['Stand']);
echo "</td>\n";
echo "</tr>";
} elseif .......
Behind the Elseif there is only the remaining code for the negative values and is equal values.
CodePudding user response:
You're always comparing with $dArray['0']['Stand']
. If you want to compare with the previous row, you need to save that in a variable at the bottom of the loop, and compare $kurs['Stand']
with that.
$prev_stand = $dArray[0]['Stand'];
foreach ($kurse as $kurs) {
$curr_stand = $kurs['Stand'];
if ($curr_stand < $prev_stand) {
$class = "class='negativ'";
} elseif ($curr_stand > $prev_stand) {
$class = "class='positive'";
} else {
$class = "";
}
echo "<tr>\n";
echo "<td $class>";
echo($kurs['Symbol']);
echo "</td>\n";
echo "<td $class>";
echo($kurs['Datum']);
echo "</td>\n";
echo "<td $class>";
echo($kurs['Stand']);
echo "</td>\n";
echo "</tr>";
$prev_stand = $curr_stand;
}
CodePudding user response:
Instead of using id you should use
<td class="color-green">
And then define a CSS you want