php - Tell me there's a better way to turn these checkboxes into JSON -


I am working on a form that allows people to choose their preferred methods of communication. Among other things, there are 9 checkboxes in this form (3 sets of 3) that I am trying to store JSON in my database.

Here are the relevant parts of the form ...

  & lt; H1 style = "padding-top: 25px;" & Gt; Communication Preferences & lt; / H1> & Lt; Hr / & gt; & Lt; Div class = "ondard contact prefix" style = "width: 28%" & gt; & Lt; H4 & gt; Preferred method & lt; / H4 & gt; & Lt; P & gt; From time to time, you may need to contact us regarding our service, which mode of contact would you like? & Lt; / P & gt; & Lt; P & gt; & Lt; Input type = "checkbox" name = "favorite []" value = "p" style = "display: inline;" / & Gt; Phone  Email & lt; Br / & gt; & Lt; Input type = "checkbox" name = "favorite []" value = "s" style = "display: inline;" /> SMS & lt; / P & gt; & Lt; / Div & gt; & Lt; Div class = "onirdirdpreprefs" style = "width: 28%; border-left: solid 1px #cCDCDCD; border-right: solid 1px #cdcdcd; padding-left: 18px; padding-right: 15px;" & Gt; & Lt; H4 & gt; Weather delays & lt; / H4 & gt; & Lt; P & gt; We do not mess with Mother Nature, and sometimes it compels us to cancel the service. If this happens then how should we inform you? & Lt; / P & gt; & Lt; P & gt; & Lt; Input type = "checkbox" name = "weather []" value = "p" style = "display: inline;" / & Gt; Phone & lt; Br / & gt; & Lt; Input type = "checkbox" name = "weather []" value = "e" style = "display: inline;" Check /> Email & lt; Br / & gt; & Lt; Input type = "checkbox" name = "weather []" value = "s" style = "display: inline;" /> SMS & lt; / P & gt; & Lt; / Div & gt; & Lt; Div class = "ondard contact prefix" style = "width: 28%" & gt; & Lt; H4 & gt; Holiday reminder & lt; / H4 & gt; & Lt; P & gt; If you want us to send a reminder about the interruption of the service due to the holidays, just choose your preferred method below. & Lt; / P & gt; & Lt; P & gt; & Lt; Input type = "checkbox" name = "holiday []" value = "p" style = "display: inline;" / & Gt; Phone & lt; Br / & gt; & Lt; Input type = "checkbox" name = "holiday []" value = "e" style = "display: inline;" Check /> Email & lt; Br / & gt; & Lt; Input type = "checkbox" name = "holiday []" value = "s" style = "display: inline;" /> SMS & lt; / P & gt; & Lt; / Div & gt; & Lt ;! - End contact preferences - & gt;  

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

What I am doing here, but it seems very messy tell me a better way ...

  / * It should be a better way / / / favorite Data $ favorite = $ this- & gt; Input-> Post ('favorite'); If (in_array ('p', $ favorite)) $ $ pref ['p'] = 1; } And {$ pref ['p'] = 0;} if (in_array ('e', $ preference)) $ $ pref ['e'] = 1; } And {$ pref ['e'] = 0;} if (in_are ('s', $ favorite)) $ $ pref ['s'] = 1; } And {$ pref ['s'] = 0;} // weather data get $ season- $ this- & gt; Input-> Post ('weather'); If (in_array ('p', $ season)) {$ wea ['p'] = 1; } And {$ wea ['p'] = 0;} if (in_array ('e', $ season)) {$ wea ['e'] = 1; } And {$ wea ['e'] = 0;} if (in_array ('s', $ season)) {$ wea ['s'] = 1; } And {$ wea ['s'] = 0;} // holidays data $ vacation = $ this- & gt; Input-> Get post ('holiday'); If (in_array ('p', $ leave)) {$ hol ['p'] = 1; } And {$ hol ['p'] = 0;} if (in_array ('e', $ leave)) {$ hol ['e'] = 1; } And {$ hol ['e'] = 0;} if (in_array ('s', $ break)) {$ hol ['s'] = 1; } And {$ hol ['s'] = 0;} $ contact_prefs = array ('favorite' = & gt; $ pref, 'weather' = & gt; $ wea, 'holidays' = & gt; $ hol); $ Contact_prefs = json_encode ($ contact_prefs);  

Thanks in advance for insights.

Make an idea more automated ( untested ):

  $ contact_prefs = array (); $ Groups = array ('favorite', 'weather', 'vacation'); // checkbox group foreign currency ($ group $ group) {$ preferences = array ('P' => 0, 'E' => 0, 's' =>;); // Create the default array $ value = $ this- & gt; Input-> Post ($ group); // Get the values ​​checked for a group foreign currency ($ value $ value) {$ preferences [$ value] = 1; // If the box is checked, the set value is 1} $ contact_prefs [$ group] = $ Preferences; } $ Contact_prefs = json_encode ($ contact_prefs);  

Of course this only works, if there are equal values ​​in three checkbox groups.


Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -