$button = $button == TRUE ? "<span class='button_join'>Join</span>" : "";
Basically, if button = TRUE
, then show up the button. But the buttons is always showing when I have $button == TRUE ?
on, but I never see it getting true anywhere?
Mysql results showing for 2 rows "In Progress", and for 1 row "Available".
But the problem is, that the button shows for every row, Ill post a picture so you will know what I am talking about:
img http://gyazo.com/4ed23f9793c6f69812d6140f64cea2a2.png
What's wrong?
This is the code:
while ($row = $query->fetch(PDO::FETCH_ASSOC))
{
if (!Ping::remote($row['server_ip'], $row['server_port']))
{
$status = "Offline";
}
else
{
$status = $row['server_status'];
}
if ($status != "In Progress" || $status != "Offline" || $status != "Full" || $status == "Available")
{
$joinButton = TRUE;
}
else
{
$joinButton = FALSE;
}
Template::drawTableRow (
$row['server_name'],
$row['server_players'],
$row['server_map'],
$row['server_status'],
$joinButton
);
}
drawTableRow
:
public static function drawTableRow($name, $players, $map, $status, $button)
{
$button = $button == TRUE ? "<span class='button_join'>Join</span>" : "";
$status = $status == "Full" || $status == "In Progress" || $status == "Offline" ? "<span class='status_error'>".$status."</span>" : "<span class='status_success'>".$status."</span>";
echo
'
<tr>
<td>
'.$name.'
</td>
<td>
'.$players.'
</td>
<td>
'.$map.'
</td>
<td>
'.$status.'
</td>
<td>
'.$button.'
</td>
</tr>
';
}
What's wrong there?
You need parenthesis:
$button = ($button == TRUE) ? "<span class='button_join'>Join</span>" : "";
Not empty sting is still == TRUE, so to make button only show for TRUE you must use === to use strict comprising. To put it simply, just do this:
$button = ($button === TRUE) ? "<span class='button_join'>Join</span>" : "";
Yes the problem is here, this will always give you TRUE:
if ($status != "In Progress" || $status != "Offline" || $status != "Full" || $status == "Available")
You probably wanted to do this :)
if (($status != "In Progress" && $status != "Offline" && $status != "Full") || $status == "Available")
A non-empty string in PHP is equal to Boolean true, and you're using the same variable to hold both your HTML snippet (a non-empty string) and a Boolean indicating whether to show it, so as soon as the condition evaluates true once, it will continue to do so forever after. Store your Boolean in a differently named variable, and the problem ceases to exist. You're also well advised to parenthesize defensively when using the ternary operator, so that you can tell at a glance which value will eventually be returned, as in this example:
$button = (($button_visible == TRUE) ? "<span class='button_join'>Join</span>" : "");
Update: Well, if that doesn't work, why not try this?
public static function drawTableRow($name, $players, $map, $status, $showJoinButton)
{
$button = '';
if ($showJoinButton) {
$button = "<span class='button_join'>Join</span>";
};
$status = $status == "Full" || $status == "In Progress" || $status == "Offline" ? "<span class='status_error'>".$status."</span>" : "<span class='status_success'>".$status."</span>";
echo
'
<tr>
<td>
'.$name.'
</td>
<td>
'.$players.'
</td>
<td>
'.$map.'
</td>
<td>
'.$status.'
</td>
<td>
'.$button.'
</td>
</tr>
';
}
If that doesn't behave the way you want it to, then what's happening is that wrong values are getting passed to drawTableRow()
, not that they're being mishandled once there.
Problem was:
First of all, I used flags ( ||
), secondary:
The IP i've used in the database, was not reachable, therefore it always end up being "Offline".
Hence when I changed all ||
to &&
and then added a reachable IP, it worked.