选择codeigniter上的预设货币值

时间:2016-03-17 08:13:01

标签: php codeigniter

我有一个货币1和2的选择框,货币1是欧元。我希望我的选择框预设值1,即欧元。

我正在使用控制器来获取这样的货币信息。

$data =  array(
            'sexList' => $this->localization->getTextParamValues(null, 'user', 'Sex'),
            'countryList' => $this->localization->getCountryList(),
            'currencyList' => $this->localization->getCurrencyList(),
            'languageList' => $this->localization->getLanguageList(),
            'user' => $userExist,
            'userinfo' => $this->user_profile->getUserInfo(),
            'Currency' => $this->localization->getCompanyCurrency()
        );

        $this->load->view('header', $this->history->getPreviousPageInArray());
        $this->load->view('borrow_registerpage_view',$data);

货币将值1推送到数据数组,但在html文件中它不会自动预设。不知道出了什么问题。

这是HTML代码,我需要预设此表中的值1,以便正常选择1,即EURO。

<?php   
  echo '<select name="currency_select" id="my_profile_select_currency" class="form-control country_style">';    
    if(!($user['Currency'] > 0)){
       echo '<option value="0">'.lang("myprofile_pinfo_no_currency_set").'</option>';
          foreach ($currencyList as $currency){
             echo '<option value="' . $currency->ID. '">' . $currency->Name . '</option>';
               }
          }else{
                              foreach ($currencyList as $currency){
                                  echo '<option value="' . $currency->ID. '"';
                                  if($currency->ID==$user['Currency']){
                                      echo 'selected';
                                  }
                                  echo '>' . $currency->Name .'';       
                              echo  '</option>';            
                              }
                      }         
                      echo '</select>'  
                      ?>

2 个答案:

答案 0 :(得分:0)

您需要更改视图

$opt = array();
foreach ($currencyList as $currency){
       $opt[$currency->ID] = $currency->Name;
}

echo form_dropdown('currency_select', $opt, set_value('currency_select', $user['Currency'], 'id="my_profile_select_currency" class="form-control country_style"');

答案 1 :(得分:0)

试试这个。仅当$user['Currency']大于0

时,才会显示 <?php echo '<select name="currency_select" id="my_profile_select_currency" class="form-control country_style">'; if(!($user['Currency'] > 0)){ echo '<option value="0">'.lang("myprofile_pinfo_no_currency_set").'</option>'; foreach ($currencyList as $currency){ echo '<option value="' . $currency->ID. '">' . $currency->Name . '</option>'; } }else{ foreach ($currencyList as $currency){ echo '<option value="' . $currency->ID. '" '.$currency->Name == 'EURO' ? 'selected="yes"' : ''.'>' .$currency->Name. '</option>'; } } echo '</select>' ?>
$currency->Name == 'EURO' ? 'selected="yes"' : ''

如果您在两种情况下都需要,可以在foreach

中使用{{1}}