1

I'm creating a carousel/image slider plugin for WordPress and I've hit a wall. There's going to be an indefinite amount of user input and I need to know how to handle this.

I currently have six static inputs: transition_time, loop_carousel, stop_on_hover, reverse_order, navigation_arrows, and show_pagination and the variable amount of info will come from the images the user wants to use. So this could be anywhere from zero to infinite.

I want to be able to create/delete X amount of columns in the DB.

So starting out there will be zero images, meaning six columns. If a user adds two images I want to have eight columns, two created. If the user deletes them then I want to go back to my original six.

I'm guessing this is possible but how and is this a good idea or should I just have a set amount of images?

4
  • 1
    You should look into entity value attribute database structure, not how to dynamically create and remove columns. Commented Mar 4, 2018 at 3:15
  • I'd be careful with using EAV. A lot can go wrong. Read these: one, two. cc @LawrenceCherone Commented Mar 4, 2018 at 3:24
  • 1
    I don't believe everything I read, if used right it works fine. How else would you have many items with many properties with many values like, for example, a custom forms, with dynamically created selects, inputs, radio buttons and inputs with whom many people fill in the same form? I know what most would do is json encode/serialise it (wordpress).. Commented Mar 4, 2018 at 3:28
  • @LawrenceCherone Right, I agree. I had to research this a lot for a client that wanted to have flexibility in an entity's fields, and while EAV looked attractive, it would've brought some problems down the road. It ended up being the right decision. Like you said, if used right it works fine. Just wanted input from someone who knows about it. Cheers. Commented Mar 4, 2018 at 5:17

1 Answer 1

2

You Are Doing It Wrong™. Changing a table definition should be an exceptional event.

Use two tables, one to model the Carousel, one to store image information, then link them.

Table Carousel:

  • id
  • [more fields here]

Table Image:

  • id
  • carousel_id (reference to the containing Carrousel)
  • [more fields]
Sign up to request clarification or add additional context in comments.

2 Comments

Keywords for your research: "one-to-many relationship", "many-to-many relationship", "foreign key", "database normalization"
This sounds like the way I should go. I wanted to avoid creating more than one table but what can you do huh?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.