2010-07-27 3 views
0

저는 사람들이 선호하는 연락 방법을 선택할 수 있도록 양식을 만들고 있습니다. 무엇보다도 양식은 JSON으로 데이터베이스에 저장하려고하는 9 개의 체크 박스 (3 세트 3 개)로 구성됩니다. 내가 언급 한 바와 같이이 체크 박스를 JSON으로 바꾸는 더 좋은 방법이 있다고 말해주세요

여기

<h1 style="padding-top:25px;">Communication Preferences</h1><hr /> 
<div class="onethird contactprefs" style="width: 28%"> 
    <h4>Preferred Method</h4> 
    <p>From time to time, we might need to contact you regarding our service. Which mode of contact would you prefer?</p> 
    <p> 
    <input type="checkbox" name="preferred[]" value="p" style="display: inline;" />Phone <!-- make these tooltips --> 
    <br /> 
    <input type="checkbox" name="preferred[]" value="e" style="display: inline;" checked />Email 
    <br /> 
    <input type="checkbox" name="preferred[]" value="s" style="display: inline;" />SMS 
    </p> 
</div> 

<div class="onethird contactprefs" style="width: 28%; border-left: solid 1px #cdcdcd; border-right: solid 1px #cdcdcd; padding-left: 18px; padding-right: 15px;"> 
    <h4>Weather Delays</h4> 
    <p>We don't mess with Mother Nature, and sometimes she forces us to cancel service. If that happens, how should we inform you?</p> 
    <p> 
    <input type="checkbox" name="weather[]" value="p" style="display: inline;" />Phone 
    <br /> 
    <input type="checkbox" name="weather[]" value="e" style="display: inline;" checked />Email 
    <br /> 
    <input type="checkbox" name="weather[]" value="s" style="display: inline;" />SMS 
    </p> 
</div> 

<div class="onethird contactprefs" style="width: 28%"> 
    <h4>Holiday Reminders</h4> 
    <p>If you'd like us to send you reminders about interruptions in service due to holidays, just choose your preferred method below.</p> 
    <p> 
    <input type="checkbox" name="holiday[]" value="p" style="display: inline;" />Phone 
    <br /> 
    <input type="checkbox" name="holiday[]" value="e" style="display: inline;" checked />Email 
    <br /> 
    <input type="checkbox" name="holiday[]" value="s" style="display: inline;" />SMS 
    </p> 

</div> 

<!-- end contact preferences --> 

... 양식의 관련 부분이다, 나는이 체크 박스에서 데이터를 가지고 잘 형성 JSON 문자열로 변환해야합니다.

내가하고있는 일은 다음과 같습니다.하지만 너무 지저분 해 보입니다. Theres 더 나은 방법을 말해 ...

/* THERE MUST BE A BETTER WAY TO DO THIS */ 
// get the preferred data 
$preferred = $this->input->post('preferred'); 

if(in_array('p',$preferred)){ 
    $pref['p'] = 1; 
}else{ $pref['p'] = 0;} 

if(in_array('e',$preferred)){ 
    $pref['e'] = 1; 
}else{ $pref['e'] = 0;} 

if(in_array('s',$preferred)){ 
    $pref['s'] = 1; 
}else{ $pref['s'] = 0;} 

// get the weather data 
$weather = $this->input->post('weather'); 

if(in_array('p',$weather)){ 
    $wea['p'] = 1; 
}else{ $wea['p'] = 0;} 

if(in_array('e',$weather)){ 
    $wea['e'] = 1; 
}else{ $wea['e'] = 0;} 

if(in_array('s',$weather)){ 
    $wea['s'] = 1; 
}else{ $wea['s'] = 0;} 

// get the holiday data 
$holiday = $this->input->post('holiday'); 

if(in_array('p',$holiday)){ 
    $hol['p'] = 1; 
}else{ $hol['p'] = 0;} 

if(in_array('e',$holiday)){ 
    $hol['e'] = 1; 
}else{ $hol['e'] = 0;} 

if(in_array('s',$holiday)){ 
    $hol['s'] = 1; 
}else{ $hol['s'] = 0;} 

$contact_prefs = array('preferred' => $pref,'weather' => $wea,'holiday' => $hol); 
$contact_prefs = json_encode($contact_prefs); 

통찰력에 미리 감사드립니다.

+1

체크 박스를 JSON으로 바꾸는 더 좋은 방법이 있습니다. – bta

답변

1

하나의 아이디어가 더 자동 (안된)를 만들려면 : 물론

$contact_prefs = array(); 
$groups = array('preferred', 'weather', 'holiday'); // checkbox groups 

foreach($groups as $group) { 
    $preferences = array('p' => 0, 'e' => 0, 's' => 0); // create default array 
    $values = $this->input->post($group); // get the checked values for a group 
    foreach($values as $value) { 
     $preferences[$value] = 1; // if box is checked, set value to 1 
    } 
    $contact_prefs[$group] = $preferences; 
} 

$contact_prefs = json_encode($contact_prefs); 

이 유일한 작품, 세 개의 체크 박스 그룹이 동일한 값이있는 경우.

+0

세 그룹의 값이 같습니다. 빠른 테스트 후에 $ type이 정의되지 않았기 때문에 7 행에서 오류가 발생합니다. 나는 $ type을 $ group으로 바꿨지 만 그것이 내가 원하는 것을 정확히주지는 못했다. 그래도 환상적인 시작입니다. 이것에 대해 머리 숙여지기까지는 몇 분이 걸릴 것입니다.하지만 제가 찾고있는 것을 줄 것이라고 생각합니다. 감사! 그것이 작동 할 때 나는 최종 결과를 게시 할 것이다. – Ryan

+0

@ Ryan : 네, 변수의 이름을 바꾸었고 그 변수를 놓쳤음에 틀림 없습니다. 실제로 그것은 당신과 같은 가치를 창출해야합니다. –

+0

아주 가깝지만, 어떻게 든 $ 환경 설정 배열은 foreach의 마지막 실행으로 덮어 쓰게됩니다. 내 날씨는 다음과 같습니다. { "선호": { "p": 1, "e": 0, "s": 0}, "날씨": { "p": 0, "e" "0", "휴일": { "p": 0, "e": 0, "s": 1}} 다음은 귀하가 생산 한 것입니다 : { "preferred": { "p" 0 ","e ": 0,"s ": 1},"휴일 ": {"p ": 0,"e " "e": 0, "s": 1}} – Ryan

관련 문제