tags:

views:

37

answers:

1

I'm working on a form that lets folks choose their preferred methods of contact. Among other things, the form consists of 9 checkboxes (3 sets of three) that I'm trying to store in my database as JSON.

Here's the pertinent parts of the form...

<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 -->

As I mentioned, I need to take the data from these checkboxes and convert them into a well formed JSON string.

Here's what I'm doing, but it seems awfully messy. Tell me theres a better way...

/* 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);

Thanks in advance for the insight.

+1  A: 

One idea to make it more automatic (untested):

$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);

Of course this only works, if the three checkbox groups have the same values.

Felix Kling
The three groups do have the same values. After a quick test, I'm getting an error on line 7 because $type is undefined. I changed $type to $group, but that didn't give me exactly what I was looking for. This is a fantastic start though. It'll take me a few minutes to get my head around this, but I think it will give me what I'm looking for. Thanks! I'll post the final result when it works.
Ryan
@Ryan: yea, I changed the name of the variables and must have missed that one. Actually it should generate the same value as yours.
Felix Kling
@Felix: it's very close, but somehow the $preferences array is getting overwritten by the final run of the foreach.Here's what mine produces: {"preferred":{"p":1,"e":0,"s":0},"weather":{"p":0,"e":1,"s":0},"holiday":{"p":0,"e":0,"s":1}}And here's what yours produces:{"preferred":{"p":0,"e":0,"s":1},"weather":{"p":0,"e":0,"s":1},"holiday":{"p":0,"e":0,"s":1}}
Ryan
@Ryan: Ah I see. Must be something with the reference to the array. I thought it works. Check my updated version. I basically moved the assignment `$contact_prefs[$group] = $preferences;` to the bottom.
Felix Kling
Absolutely beautiful, Felix. You guys make it look so easy. Thanks.
Ryan