0

I am working on a project in PHP & MySQL, I have a Table with an expandable amount of fields. I have been looking for a way to work out an array system to handle this but am unsure. I am not looking for someone to code it for me but would like opinions on the best way to handle it.

Exp:

TABLE Stage one:
ID    OPTION1  OPTION2
1     dirt     drop
2     ground   lake
3     clay     pond

Now to explain what I am looking to do: I need to create a way to allow my arrays to see all the fields and make an array for each once so.

Exp:

$arrayone(dirt,ground,clay);
$arraytwo(drop,lake,pond);

But if I add a new field to my Database like so:

Exp:

TABLE Stage one:
ID    OPTION1  OPTION2   OPTION3
1     dirt     drop      sky
2     ground   lake      cloud
3     clay     pond      sun

I need it to see the new field and allow for another array to be created on proccess..

Exp:

$arrayone(dirt,ground,clay);
$arraytwo(drop,lake,pond);
$arraythree(sky,cloud,sun);

Like I said I have been looking but not sure what I need to be looking for if anyone know and if it can be done could you point me to some references I can read up on to create.

I have this EXP:

$stringfromdb = "1,2,3,4";
$stringfromdb = "5,6,7,8";

Now i want to my php to take these two strings and build Arrays like

$arrayone(1,5);
$arraytwo(2,6);
$arraythree(3,7);
$arrayfour(4,8);

But if i was to add more to the string later like

$stringfromdb = "1,2,3,4,9,10";
$stringfromdb = "5,6,7,8,11,12";

I would need it to creat new arrays based off the new added sections of the string..

$arrayone(1,5);
$arraytwo(2,6);
$arraythree(3,7);
$arrayfour(4,8);
$arrayfive(9,11);
$arraysix(10,12);

I do plan on using some form of explode(); to split the scripts at the separators. Not sure if this helps at all.. to get my issue across lol sorry...

3
  • An expandable amount of field? What's that? Doesn't sound good. SEE NORMALIZATION Commented Jul 26, 2014 at 14:23
  • right now i have just 6 fields, but i want to be able to add new fields with out reediting the code... Commented Jul 26, 2014 at 14:31
  • U should use a cross reference table for this. One table containing the options (option_id, title) , one table containing the values (value_id, title) and one table for combining them (option_id, value_id) Commented Jul 26, 2014 at 14:32

2 Answers 2

1

Not really sure I understood your goal entirely, but I've worked on a project with similar structure, and it was done like so:

id  | field | value
----+-------+------
1   | opt1  | dirt
1   | opt2  | ground
1   | opt3  | clay
2   | opt1  | apple
2   | opt2  | banana
2   | opt3  | melon

All snugly fits into single table. The table has a composite primary key: (id, field). It might also be a good idea to have an index on those two columns.

Since you have a fixed number of columns, adding a "field" is as easy as adding one entry for each ID.

You can also have a "structure" table describing how many and what fields are required for the items.

Obviously, the SQL queries will not be as simple as with a simple table you have now, but it is much more practical.

Sign up to request clarification or add additional context in comments.

2 Comments

I'd use 3 tables over 1 table to reduce redundancy of data
Yeah, it would make sense to have a fields table with numeric IDs for the fields, I guess. It could make it more efficient, but also adds complexity.
0

Sounds like a normal two dimensional array to me - $array = array(array('dirt', 'ground', 'clay'), array('drop', 'lake', 'pond')...), unless I didn't understand the question...

Comments

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.