在php中预设选择值

时间:2016-08-11 09:43:21

标签: php codeigniter

嗨我有一个信息列表,我从控制器发送到这个页面

 [creditCardList] => array(6) (
    [0] => stdClass object {
      text_code => (string) 1
      text_value => (string) Visa
    }
    [1] => stdClass object {
      text_code => (string) 2
      text_value => (string) MasterCard
    }
    [2] => stdClass object {
      text_code => (string) 3
      text_value => (string) Eurocard
    }
    [3] => stdClass object {
      text_code => (string) 4
      text_value => (string) Amex
    }
    [4] => stdClass object {
      text_code => (string) 5
      text_value => (string) Diners
    }
    [5] => stdClass object {
      text_code => (string) 6
      text_value => (string) Other
    }
  )

在html页面中,我想预设加载的值是text_code 1,即VISA,但我无法实现。这是html代码。 我尝试写$creditCardList->text_code[1]但没有用。

<select name="credit_card_select" id="my_profile_select_credit_card" class="form-control country_style">
                <?php
                  if(!($userriskrate['credit_card_type'] > 0)){
                    echo '<option value="' . $creditCardList->text_code[1]. '">' . $creditCard->text_value . '</option>';
                    foreach ($creditCardList as $creditCard){
                        echo '<option value="' . $creditCard->text_code. '">' . $creditCard->text_value . '</option>';
                    }
                  }else{
                    foreach ($creditCardList as $creditCard){
                        echo '<option value="' . $creditCard->text_code. '"';
                        if($creditCard->text_code == $userriskrate['credit_card_type']){
                            echo 'selected';
                        }
                        echo '>' . $creditCard->text_value .'';     
                        echo  '</option>';          
                    }
                  }
                  ?> 
                </select>            

2 个答案:

答案 0 :(得分:1)

你应该使用它作为creditcard [i],因为它有一个数组()。

 echo '<option value="' . $creditCardList[0]->text_code. '">' . $creditCardList[0]->text_value . '</option>';

这将给你签证作为签证的价值。

显示除预选值

以外的值
              <?php
              if(!($userriskrate['credit_card_type'] > 0)){
                echo '<option value="' . $creditCardList[0]->text_code. '">' . $creditCard[0]->text_value . '</option>';
                 $presetvalue = $creditCardList[0]->text_code;
                foreach ($creditCardList as $creditCard){

                    if($presetvalue !== $creditCard->text_code)
                    {  
                    echo '<option value="' . $creditCard->text_code. '">' . $creditCard->text_value . '</option>';

                    }
                }  
             }     

答案 1 :(得分:0)

您的错误在于:echo '<option value="' . $creditCardList->text_code[1]. '">' . $creditCard->text_value . '</option>';

需要:

echo '<option value="' . $creditCardList[0]->text_code. '">' . $creditCard[0]->text_value . '</option>';

我在这里重构了代码:

   if(!($userriskrate['credit_card_type'] > 0)) $selector = 'VISA'; else $selector = $userriskrate['credit_card_type'];
      foreach ($creditCardList as $creditCard){
            if($creditCard->text_value == $selector) $selected='SELECTED'; else $selected='';
            echo '<option value="' . $creditCard->text_code. ' '.$selected.'">' . $creditCard->text_value . '</option>';
      }