如果声明没有显示?

时间:2016-09-19 10:34:13

标签: php select

我有这段代码:

<?php
$filterUrl = $_SERVER['HTTP_HOST'];
?>
<select class="jsFilter" name="language">
    <option value>Sprache</option>
    <?php if (!$filterUrl == "www.domain.de"){?><option value="de" <?php if(isset($_GET['language']) && $_GET['language']=='de'){echo "selected";}?>>Deutsch</option><?php } ?>
    <?php if (!$filterUrl == "www.domain.com"){?><option value="en" <?php if(isset($_GET['language']) && $_GET['language']=='en'){echo "selected";}?>>English</option><?php } ?>
    <?php if (!$filterUrl == "www.domain.fr"){?><option value="fr" <?php if(isset($_GET['language']) && $_GET['language']=='fr'){echo "selected";}?>>Français</option><?php } ?>
    <?php if (!$filterUrl == "www.domain.nl"){?><option value="nl" <?php if(isset($_GET['language']) && $_GET['language']=='nl'){echo "selected";}?>>Nederlands</option><?php } ?>
</select>

我不知道为什么没有显示所有选项?

我希望你们能帮帮我。

4 个答案:

答案 0 :(得分:1)

您可以尝试以下代码:

<?php
$filterUrl = $_SERVER['HTTP_HOST'];
?>
<select class="jsFilter" name="language">
    <option value>Sprache</option>
    <?php if ($filterUrl != "www.domain.de"){?><option value="de" <?php if(isset($_GET['language']) && $_GET['language']=='de'){echo "selected";}?>>Deutsch</option><?php } ?>
    <?php if ($filterUrl != "www.domain.com"){?><option value="en" <?php if(isset($_GET['language']) && $_GET['language']=='en'){echo "selected";}?>>English</option><?php } ?>
    <?php if ($filterUrl != "www.domain.fr"){?><option value="fr" <?php if(isset($_GET['language']) && $_GET['language']=='fr'){echo "selected";}?>>Français</option><?php } ?>
    <?php if ($filterUrl != "www.domain.nl"){?><option value="nl" <?php if(isset($_GET['language']) && $_GET['language']=='nl'){echo "selected";}?>>Nederlands</option><?php } ?>
</select>

答案 1 :(得分:0)

<?php
    $filterUrl = $_SERVER['HTTP_HOST'];
    $language  = $_GET['language'];
?>
<select class="jsFilter" name="language">
    <option value>Sprache</option>
    <option value="de" <?php if(isset($language) && $language=='de'){echo "selected";}?>>Deutsch</option>
    <option value="en" <?php if(isset($language) && $language=='en'){echo "selected";}?>>English</option>
    <option value="fr" <?php if(isset($language) && $language=='fr'){echo "selected";}?>>Français</option>
    <option value="nl" <?php if(isset($language) && $language=='nl'){echo "selected";}?>>Nederlands</option>
</select>

答案 2 :(得分:0)

或许你可以这样试试:

    <?php
        $filterUrl  = $_SERVER['HTTP_HOST'];
        $options    = "";
        $language   = isset($_GET['language']) ? $_GET['language'] : "de";


        $options .= '<option value="de" ';
        if($language == "de"){$option .= "selected"; }
        $options .= '>Deutsch</option>';

        $options .= '<option value="en" ';
        if($language == "en"){$option .= "selected"; }
        $options .= '>English</option>';

        $options .= '<option value="fr" ';
        if($language == "fr"){$option .= "selected"; }
        $options .= '>Français</option>';

        $options .= '<option value="nl" ';
        if($language == "nl"){$option .= "selected"; }
        $options .= '>Nederlands</option>';

    ?>

答案 3 :(得分:0)

您的条件有误,如果您不想包含基于域的语言选项,它应该是这样的 if ($filterUrl !== "www.domain.de")。您的版本if (!$filterUrl == "www.domain.de")首先取消字符串,它会为您提供空字符串,然后将其进行比较,这显然是假的。

所以整个解决方案看起来像这样:

<?php
$filterUrl = $_SERVER['HTTP_HOST'];
?>
<select class="jsFilter" name="language">
    <option value>Sprache</option>
    <?php if ($filterUrl !== "www.domain.de"){?><option value="de" <?php if(isset($_GET['language']) && $_GET['language']=='de'){echo "selected";}?>>Deutsch</option><?php } ?>
    <?php if ($filterUrl !== "www.domain.com"){?><option value="en" <?php if(isset($_GET['language']) && $_GET['language']=='en'){echo "selected";}?>>English</option><?php } ?>
    <?php if ($filterUrl !== "www.domain.fr"){?><option value="fr" <?php if(isset($_GET['language']) && $_GET['language']=='fr'){echo "selected";}?>>Français</option><?php } ?>
    <?php if ($filterUrl !== "www.domain.nl"){?><option value="nl" <?php if(isset($_GET['language']) && $_GET['language']=='nl'){echo "selected";}?>>Nederlands</option><?php } ?>
</select>