if (mysqli_num_rows($result) > 0) {
function duration() {
if ($row["duration"] == 0) {
return "Expired";
} else if ($row["duration"] > -1) {
return $row["duration"];
} else {
return "Unknown Error";
}
};
echo '<table>';
while($row = mysqli_fetch_assoc($result)) {
timeLeft();
echo '<tr><td>' . " Left from event: " . duration() . '</td></tr>';
}
echo '</table>';
} else {
echo "0 results";
};
mysqli_close($conn);
我正在尝试用“Expired”替换0,但是使用此代码它将替换所有表。
而不是:
Left from event: Expired Left from event: 3
我明白了:
Left from event: Expired Left from event: Expired
答案 0 :(得分:0)
您需要将$row
传递给函数:
function duration(array $row) {
if ($row["duration"] == 0) {
return "Expired";
} else if ($row["duration"] > -1) {
return $row["duration"];
} else {
return "Unknown Error";
}
}
并使用duration($row)
作为一个完整的例子,
$rows = [
[ 'duration' => 0 ],
[ 'duration' => 2 ],
[ 'duration' => 3 ],
[ 'duration' => 4 ]
];
function duration(array $row) {
if ($row["duration"] == 0) {
return "Expired";
} else if ($row["duration"] > -1) {
return $row["duration"];
}
}
foreach ($rows as $row) {
$duration = duration($row);
echo "Left from event: {$duration}\n";
}
产生
Left from event: Expired
Left from event: 2
Left from event: 3
Left from event: 4