1

I have the following bootstrap dropdown. Using PHP I get the country of the visitor. So if the visitor comes from England or any other country not listed below, the English must be shown, on top and be removed from the list. as shown below. I can do the PHP side, but what about the javascript since this is not a select dropdown and there is not a selected ?

Standard version:

    <li class="dropdown">
        <a href="#" data-toggle="dropdown" class="dropdown-toggle">English <b class="caret"></b></a>
        <ul class="dropdown-menu">
            <li><a href="gr.html">Greek</a></li>
            <li><a href="it.html">Italian</a></li>
            <li><a href="cz.html">Czech</a></li>
        </ul>
    </li>

When a visitor from the UK comes in:

    <li class="dropdown">
        <a href="#" data-toggle="dropdown" class="dropdown-toggle">English <b class="caret"></b></a>
        <ul class="dropdown-menu">
            <li><a href="gr.html">Greek</a></li>
            <li><a href="it.html">Italian</a></li>
            <li><a href="cz.html">Czech</a></li>
        </ul>
    </li>

Here is what the HTML will be when a Greek enters:

    <li class="dropdown">
        <a href="#" data-toggle="dropdown" class="dropdown-toggle">Greek<b class="caret"></b></a>
        <ul class="dropdown-menu">
            <li><a href="en.html">English</a></li>
            <li><a href="it.html">Italian</a></li>
            <li><a href="cz.html">Czech</a></li>
        </ul>
    </li>
EnexoOnoma
  • 8,454
  • 18
  • 94
  • 179

1 Answers1

1

This should work for you:

<?php 

    $country  = "Italian";
    $countrys = array("en" => "English", "gr" => "Greek", "it" => "Italian", "cz" => "Czech");
    $default = $countrys["en"];

?>

<li class="dropdown">
    <a href="#" data-toggle="dropdown" class="dropdown-toggle"><?php echo (($key = array_search($country, $countrys)) !== FALSE ? $countrys[$key] : $country = $default); ?><b class="caret"></b></a>
    <ul class="dropdown-menu">
    <?php foreach(array_diff($countrys, array($country)) as $k => $v): ?>
        <li><a href="<?php echo $k; ?>.html"><?php echo $v; ?></a></li>
    <?php endforeach; ?>
    </ul>
</li>

EDIT:

If you get the country shortcut and not the full name just use this:

<?php 

    $country  = "gr";
    $countrys = array("en" => "English", "gr" => "Greek", "it" => "Italian", "cz" => "Czech");
    $default = "en";

?>

<li class="dropdown">
    <a href="#" data-toggle="dropdown" class="dropdown-toggle"><?php echo (in_array($country, array_flip($countrys)) !== FALSE ? $countrys[$country] : $countrys[$country = $default]); ?><b class="caret"></b></a>
    <ul class="dropdown-menu">
    <?php foreach(array_diff_key($countrys, array_flip(array($country))) as $k => $v): ?>
        <li><a href="<?php echo $k; ?>.html"><?php echo $v; ?></a></li>
    <?php endforeach; ?>
    </ul>
</li>
Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • Thank you for this. To clear my mind, if the visitors country is not included in the array, will show the en ? – EnexoOnoma Feb 19 '15 at 13:59
  • @Xalloumokkelos Yes, just try it and you will see it :D (e.g. `$country = "asdsfg";`) – Rizier123 Feb 19 '15 at 14:00
  • I think I am missing something... My country is `$country = "CY"` . So I must get the english language since it is not listed on the array. And I get it. However, if I replace for example the `IT` with `CY` it was supposed to show `Italian` but not... – EnexoOnoma Feb 19 '15 at 14:06
  • @Xalloumokkelos So the country which you get are the shortcuts not the full names? e.g. you get/have: `en, gr` and NOT `English, Greek` ? – Rizier123 Feb 19 '15 at 14:08
  • Yes, sorry for not clarifying this earlier – EnexoOnoma Feb 19 '15 at 14:21
  • @Xalloumokkelos updated my answer. Hope this solves your problem – Rizier123 Feb 19 '15 at 14:34
  • @Xalloumokkelos You're welcome! Have a nice day :D (BTW: Just out of curiosity: Why did you never accepted or wrote a comment on this one: http://stackoverflow.com/q/25844992/3933332 ? For me it also seems that you used my code in another question: http://stackoverflow.com/q/27698009/3933332 ; So was there a correct answer?) – Rizier123 Feb 19 '15 at 14:45
  • I don't remember to be honest, but I accepted it now :) – EnexoOnoma Feb 19 '15 at 14:48