How to save multiple repeatable fields as array in database?

别说谁变了你拦得住时间么 提交于 2019-12-13 05:58:21

问题


On the theme im working i'm trying to make slideshow. I have everything set so far i just don't understand how to save them in database when there are multiple slides.

I will skip the code as it's very large ill just add example of input values.

Each slide is made of image upload field(which is basically text field that holds link to image) and textarea field for the text in slide.

So values of the slide are like this.

Slide 1 -> $slide[image][0]  -- $slide[text][0]

Slide 2 -> $slide[image][1]  -- $slide[text][1]

Slide 3 -> $slide[image][2]  -- $slide[text][2]

It's a WordPress theme so wordpress it self is capable to save an array when it recognize it. And if anyone is familiar theme is built upon Options Framework.

When i save this, this is what i get in database.

a:1:{s:13:"slide_example";a:1:{s:5:"image";s:0:"";}}

when (i guess) it should be like this

a:1:{s:13:"slide_example";a:1:{s:5:"image";s:0:"slide text";};a:2:{s:5:"image";s:0:"slide text";};a:3:{s:5:"image";s:0:"slide text";}}

回答1:


Your values should be setup like this:

Slide 1 -> $slide[0][image]  -- $slide[0][text]

Slide 2 -> $slide[1][image]  -- $slide[1][text]

Slide 3 -> $slide[2][image]  -- $slide[2][text]



回答2:


I made it working. Im not sure if this is proper approach but it's working for now.

I used array_map.

$slider = array_map(null, $slide['image'], $slide['editor'] );


来源:https://stackoverflow.com/questions/21050050/how-to-save-multiple-repeatable-fields-as-array-in-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!