vimes1984
vimes1984

Reputation: 1693

php array and returning a default value?

Here is my code:

<div class="search-menu">
  <div class="btn-group fc">
    <a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
      <?php
        $currencies = explode(',', hana_get_opt('_cc'));
        foreach ($currencies as $currency) {
          if ($currency == get_option('woocommerce_currency')){
            echo $currency;
            break;
          }else{
            echo "Select Currency";
            break;
          }
        }
      ?>
      <span class="caret"></span>
    </a>
    <ul class="dropdown-menu currency_switcher">
      <?php
        foreach ($currencies as $currency) {
          if ($currency == get_option('woocommerce_currency'))
            echo '<li><a href="#" class="reset default" data-currencycode="' . $currency . '">' . $currency . '</a></li>';
          else
            echo '<li><a href="#" data-currencycode="' . $currency . '">' . $currency . '</a></li>';
        }
      ?>
    </ul>
  </div>
</div>

Which works great it returns a list of my currencies and updates the page accordingly, I have just on question if the user has yet to set a value I would like it to say either select currency or just apply the first option from my hana_get_opt('_cc') array as the default. Here is the html generated code:

<div class="btn-group fc">
                        <a class="btn dropdown-toggle" data-toggle="dropdown" href="#">undefined<span class="caret"></span></a>
                        <ul class="dropdown-menu currency_switcher">
  <li><a href="#" class="reset default active" data-currencycode="SEK">SEK</a></li>
  <li><a href="#" data-currencycode="EUR">EUR</a></li>                                
                        </ul>
                    </div>

I am no php coder and much appreciate any help provided Chris

Upvotes: 0

Views: 353

Answers (2)

MatthiasLaug
MatthiasLaug

Reputation: 2914

based on the assumption that get_option('woocommerce_currency') would return null if no currency is selected

<div class="search-menu">
<div class="btn-group fc">
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
  <?php
    $currencies = explode(',', hana_get_opt('_cc'));
    $currentCurrency = get_option('woocommerce_currency')
    if ($currentCurrency === false) {
        echo "select currency";
    }
    else{
        echo $currentCurrency;
    }
  ?>
  <span class="caret"></span>
</a>
<ul class="dropdown-menu currency_switcher">
  <?php
    foreach ($currencies as $currency) {
      if ($currency == $currentCurrency)
        echo '<li><a href="#" class="reset default" data-currencycode="' . $currency . '">' . $currency . '</a></li>';
      else
        echo '<li><a href="#" data-currencycode="' . $currency . '">' . $currency . '</a></li>';
    }
  ?>
</ul>

replace the if clause if ( $currentCurrency != null ) with whatever applies, if get_option('woocommerce_currency') returns anything other as null if not yet set

Upvotes: 0

Ganesh RJ
Ganesh RJ

Reputation: 942

   if(get_option('woocommerce_currency')==' ')
    {
        $currencies = explode(',', hana_get_opt('_cc'));
          foreach ($currencies as $currency) 
                {

                 if $currency == get_option('woocommerce_currency'){
                     {
                     echo $currency;
                     break;
                       }
                              else{
                                        echo "Select Currency";
                                        break;
                                    }
                 }
    }

    else
    {   
      echo "Select Currency";
       $currencies = explode(',', hana_get_opt('_cc'));
     $currency=$currencies [0];
    }

Upvotes: 1

Related Questions