centerwork Posted November 7, 2008 Share Posted November 7, 2008 I was wondering what is the best way to store an array in the database? What type should I use for the field structure? Quote Link to comment https://forums.phpfreaks.com/topic/131799-solved-what-is-the-best-way-to-store-an-array-in-the-database/ Share on other sites More sharing options...
rhodesa Posted November 7, 2008 Share Posted November 7, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/131799-solved-what-is-the-best-way-to-store-an-array-in-the-database/#findComment-684661 Share on other sites More sharing options...
centerwork Posted November 7, 2008 Author Share Posted November 7, 2008 I am creating a custom product page. I would like to store a qty as the key and the price as the value. Then store that array in the database. Quote Link to comment https://forums.phpfreaks.com/topic/131799-solved-what-is-the-best-way-to-store-an-array-in-the-database/#findComment-684671 Share on other sites More sharing options...
rhodesa Posted November 7, 2008 Share Posted November 7, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/131799-solved-what-is-the-best-way-to-store-an-array-in-the-database/#findComment-684674 Share on other sites More sharing options...
centerwork Posted November 7, 2008 Author Share Posted November 7, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/131799-solved-what-is-the-best-way-to-store-an-array-in-the-database/#findComment-684688 Share on other sites More sharing options...
fenway Posted November 7, 2008 Share Posted November 7, 2008 You can *always* use a table. Quote Link to comment https://forums.phpfreaks.com/topic/131799-solved-what-is-the-best-way-to-store-an-array-in-the-database/#findComment-684697 Share on other sites More sharing options...
centerwork Posted November 7, 2008 Author Share Posted November 7, 2008 Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/131799-solved-what-is-the-best-way-to-store-an-array-in-the-database/#findComment-684704 Share on other sites More sharing options...
rhodesa Posted November 7, 2008 Share Posted November 7, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/131799-solved-what-is-the-best-way-to-store-an-array-in-the-database/#findComment-684753 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.