0
$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
(source: gyazo.com)

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?

4

4 回答 4

1

非空字符串仍然 == TRUE,因此要使按钮仅显示为 TRUE,您必须使用 === 来使用严格包含。简单地说,只需这样做:

$button = ($button === TRUE) ? "<span class='button_join'>Join</span>" : "";

是的,问题就在这里,这总是会给你 TRUE:

if ($status != "In Progress" || $status != "Offline" || $status != "Full" || $status == "Available")

你可能想这样做:)

if (($status != "In Progress" && $status != "Offline" && $status != "Full") || $status == "Available")
于 2013-07-25T19:41:02.350 回答
0

问题是:

首先,我使用了 flags ( ||),次要的:

我在数据库中使用的 IP 无法访问,因此它最终总是“离线”。

因此,当我全部更改||&&然后添加一个可访问的 IP 时,它就起作用了。

于 2013-07-25T19:55:32.600 回答
0

You need parenthesis:

$button = ($button == TRUE) ? "<span class='button_join'>Join</span>" : "";
于 2013-07-25T19:36:51.560 回答
0

PHP 中的非空字符串等于布尔值 true,并且您使用相同的变量来保存 HTML 片段(非空字符串)和指示是否显示它的布尔值,因此只要条件评估一次为真,以后将永远如此。将您的布​​尔值存储在一个不同命名的变量中,问题就不复存在了。还建议您在使用三元运算符时用括号括起来,这样您就可以一眼看出最终将返回哪个值,如下例所示:

$button = (($button_visible == TRUE) ? "<span class='button_join'>Join</span>" : "");

更新:好吧,如果这不起作用,为什么不试试这个呢?

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>
    ';
}

如果这不符合您的要求,那么发生的情况是错误的值被传递给drawTableRow(),而不是它们在那里被错误处理。

于 2013-07-25T19:43:21.370 回答