php数组并返回默认值?

时间:2013-04-25 10:32:35

标签: php arrays

这是我的代码:

<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>

哪个很好用它返回我的货币列表并相应地更新页面,我只是在质疑用户是否还没有设置一个值我希望它说选择货币或只是应用我的第一个选项hana_get_opt('_cc')数组作为默认值。 这是html生成的代码:

<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>

我不是php编码器,非常感谢提供的任何帮助 克里斯

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

基于假设get_option('woocommerce_currency')如果未选择货币则返回null

<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>

将if子句if ( $currentCurrency != null )替换为适用的任何内容,如果get_option('woocommerce_currency')返回其他任何内容null,如果尚未设置