Jump to content

[SOLVED] What is the best way to store an array in the database?


centerwork

Recommended Posts

that is completely dependent on what is in the array and what you want to do with it...

 

if it's just an array of values, for one entry (let's say a user), and they have no relation to the 'array field' for other entries, you can serialize it and store it into a TEXT field for that entry. but this defeats the purpose of relational databases. it would be better to have a separate table, with a row for each item in that array, and a column to store the unique id of the 'user' it relates to.

 

can you describe a little more on what the data is?

Link to comment
Share on other sites

aka, if you buy 1-4 it's $5 each, if you buy 5-14 it's $4 each, and if you buy more then 15, it's $3 each?

 

I would have a products table, where you have all the details for the product. In that table, each product should have it's own product_id as an auto_increment primary key

 

Then a prices table, where each row has a product_id, min_qty, and price.

Link to comment
Share on other sites

The reason I can't use a table is that the number of quantise will be changing.

 

Product 1:

  qty  1000    2000      4000

price  100.00  200.00  300.00

 

Product 2:

  qty  1000    2000 

price  100.00  200.00

 

Product 3:

May have up to 30 QTY & Prices.

 

I guess I could format the table like this

 

p_id      p_qty    p_price

1        1000      100.00

1        2000      200.00

1        3000      300.00

2        1000      100.00

2        2000      200.00

etc...

 

Or, Does anyone else have any other suggestions?

Link to comment
Share on other sites

p_id      p_qty    p_price

1        1000      100.00

1        2000      200.00

1        3000      300.00

2        1000      100.00

2        2000      200.00

etc...

 

that is the format i would use

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.