Tuesday 15 April 2014

php - Wordpress - save similar metabox fields into array -


I have to save in the array values ​​with the same key. For example, let me name for the name 'crop-43' Need to save the value: vertical and horizontal

Input field in Metabox:

  & lt; P & gt; & Lt; Label = "crop43" & gt; 4: 3 crop status & lt; / Label & gt; & Lt; Select name = "crop43 []" style = "width: 10%" & gt; & Lt; Options & gt; Center & lt; / Options & gt; & Lt; Options & gt; Top & lt; / Options & gt; & Lt; Options & gt; Down & lt; / Options & gt; & Lt; / Select & gt; & Lt; Select name = "crop43 []" style = "width: 10%" & gt; & Lt; Options & gt; Center & lt; / Options & gt; & Lt; Options & gt; Left & lt; / Options & gt; & Lt; Options & gt; Rights & lt; / Options & gt; & Lt; / Select & gt; & Lt; / P & gt;   

Savings process:

  if (isset ($ _ POST ['crop43'])) {update_post_meta ($ post-> ID, Crop43 ', $ _POST [' crop43 ']); }   

This only saves the final information (horizontal position), but not vertical.

2 is an easy solution to rename the selection

  Lt; P & gt; & Lt; == Label for the "vertical" & 4: 3 crop status & lt; / Label & gt; & Lt; Choose name = "vertical" style = "width: 10%" & gt; & Lt; Options & gt; Center & lt; / Options & gt; & Lt; Options & gt; Top & lt; / Options & gt; & Lt; Options & gt; Down & lt; / Options & gt; & Lt; / Select & gt; & Lt; Select name = "horizontal" style = "width: 10%" & gt; & Lt; Options & gt; Center & lt; / Options & gt; & Lt; Options & gt; Left & lt; / Options & gt; & Lt; Options & gt; Rights & lt; / Options & gt; & Lt; / Select & gt; & Lt; / P & gt;   

Then you can do something like this:

  if (isset ($ _ POST ['horizontal'] & isset ($ _ POST [ $ Vertical]] $ $ array ['horizontal'] = $ _POST ['horizontal']; $ array ['vertical'] = $ _POST ['vertical']; $ crop43 = json_encode ($ array); update_post_meta ($ post -> ID, 'crop43', $ crop43);}   

Then, use json_decode when you want to change the database value back to an array.

No comments:

Post a Comment