2016-08-11 2 views
-1

안녕하세요 나는이사전 설정 값을 선택

[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 점이다 페이지에서 PLC로 전송하고 정보의 목록을 가지고 , 그것은 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>    

답변

1

배열()이 있으므로 신용 카드 [i]로 사용해야합니다.

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

이것은 비자 인 가치로 u 비자를 줄 것입니다.

가 미리 선택한 값 이외의 값을 표시하려면

   <?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>'; 

        } 
       } 
      }  
+0

@Farhana, 이건 당신을 위해 일하지 못했습니다. – coder

+0

죄송합니다. 작동하지 않아 빈 필드를 보여 주셨습니다. – FaF

+0

@Farhana 죄송합니다. creditcardList [0] -> text_value 대신 creditcard [0] -> text_value를 썼습니다. 텍스트 코드 부분이 정확했습니다. 내 나쁜 – coder

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>'; 
     } 
+0

안녕하세요 감사합니다 작품! 제한 시간이 끝났을 때 대답을 수락하고 질문을 한 번 더하고 싶습니다. 비자 카드와 마스터 카드 만 표시하도록 제한하려면 어떻게해야합니까? – FaF

+0

'$ autorizedValues ​​= array ('Visa', 'MasterCard');'와'if (! credit_cards-> text_value, $ autorizedValues)가 계속됩니다 '라는 루프에서'text_value' '$ autorizedValues' 레코드에 없으므로 건너 뜁니다 – MaximeK

관련 문제