簡體   English   中英

PHP數組並返回默認值?

[英]php array and returning a default value?

這是我的代碼:

<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編碼器,非常感謝Chris提供的任何幫助

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

基於以下假設:如果未選擇任何貨幣,則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 )替換if子句,如果get_option('woocommerce_currency')返回其他null如果尚未設置get_option('woocommerce_currency')null

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM