Brandon Benefield
Brandon Benefield

Reputation: 1672

PHP SQL: Dynamically Creating and Deleting Columns in Database

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?

Upvotes: 1

Views: 120

Answers (1)

RandomSeed
RandomSeed

Reputation: 29769

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]

Upvotes: 2

Related Questions