How to store variable length arrays in a database - php

I'm looking for a way to save data based off a set of rows in another table, but I don't know how to set up the field. Think something similar to using the results of a mysql group_concat as the field. The data is based off the unique combination of rows, rather than one row or field.
What I need to be able to do is:
Store the array itself in the database
Store associated data about the array
Retreive the array
The ability to lookup data about the array using the data in the array
Some options I've thought about:
Saving as an ordered set concatenated into a string.
Saving the serialized array (serialized using php's serialize function).
Saving the set as a hashed string using a reversible hash.
None of these options seem correct so I came here hoping someone has a better answer.
Background:
Supposed I have the following tables:
users {id, other unimportant fields}
products {id, other unimportant fields}
shipments {id, user_id, product_id, date, other unimportant fields}
I want to create a new table called assigned_products where the assigned product is based off of the unique combination of products they've received in the past. So assigned_products should look like:
assigned_products {set_of_products_received (array), product_id (data about the array)}
I don't know of a good way to store set_of_products_received in a database.
Example use:
Suppose I have 100 users who got product A, 100 users who got product B, and 100 users who got products A and B. Suppose then I wanted to give product B to everyone who got product A, product A to everyone who got product B, and product C to everyone who got product A and B. The assigned products table should look like:
+--------------------------+------------+
| set_of_products_received | product_id |
+--------------------------+------------+
| A | B |
| B | A |
| A, B | C |
+--------------------------+------------+
I'm just looking for a better way of storing set_of_products_received
Reading this over I realize it's a bit hard to understand, but I don't really know the appropriate terms to describe this issue (probably why I'm having trouble finding solutions). I'll be happy to clarify if anyone has any questions.

Related

Update Json string that stored on DB

I have DB table that one of his Column contain a JSON string like this:
{"freet1":"\u05d2'\u05dc\u05d0\u05e8\u05d8\u05d4 \u05de\u05d5\u05d3\u05d9\u05e2\u05d9\u05df - \u05de\u05d5\u05d3\u05d9\u05e2\u05d9\u05df - \u05ea\u05e4\u05e8\u05d9\u05d8 \u05de\u05e9\u05dc\u05d5\u05d7\u05d9\u05dd - Ice Cream | Tinder","freet2":"5% \u05d4\u05e0\u05d7\u05d4","freet3":"\u05dc\u05de\u05d6\u05de\u05d9\u05e0\u05d9\u05dd \u05d3\u05e8\u05da \u05d4\u05d0\u05ea\u05e8","freet6":"\u05d4\u05d4\u05d6\u05de\u05e0\u05d4 \u05e9\u05dc\u05d9","freet4":"\u05d0\u05d9\u05df \u05d0\u05e4\u05e9\u05e8\u05d5\u05ea \u05dc\u05e9\u05e2\u05e8\u05da \u05d0\u05ea \u05d4\u05d4\u05d6\u05de\u05e0\u05d4 \u05de\u05db\u05d9\u05d5\u05d5\u05df \u05e9\u05d4\u05de\u05d5\u05e6\u05e8\u05d9\u05dd \u05e9\u05e7\u05d9\u05dc\u05d9\u05dd. \u05de\u05d9\u05e0\u05d9\u05de\u05d5\u05dd \u05de\u05e9\u05dc\u05d5\u05d7 45 \u05e9\"\u05d7","0":{"menu":"\u05d2\u05dc\u05d9\u05d3\u05d5\u05ea \u05e9\u05de\u05e0\u05ea","currentmenufile":"142525634133.JPG","data":[{"product":"\u05e9\u05d5\u05e7\u05d5\u05dc\u05d3 \u05de\u05e0\u05d8\u05d4","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425056679\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425056679\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e9\u05d5\u05e7\u05d5\u05dc\u05d3 \u05d1\u05dc\u05d2\u05d9","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425056687\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425056687\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e9\u05d5\u05e7\u05d5\u05dc\u05d3 \u05dc\u05d1\u05df","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425056695\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425056695\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e9\u05d5\u05e7\u05d5\u05dc\u05d3 \u05e7\u05d9\u05e0\u05d3\u05e8","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425056702\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425056702\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e9\u05d5\u05e7\u05d5\u05dc\u05d3 \u05ea\u05e4\u05d5\u05d6","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425056727\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425056727\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e8\u05d9\u05d1\u05ea \u05d7\u05dc\u05d1","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425056815\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425056815\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05dc\u05d5\u05d8\u05d5\u05e1","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425056823\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425056823\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e7\u05e4\u05d5\u05e6'\u05d9\u05e0\u05d5","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425056830\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425056830\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d5\u05d9\u05e1\u05e7\u05d9","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425056944\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425056944\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d5\u05e0\u05d9\u05dc \u05e6\u05e8\u05e4\u05ea\u05d9","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425056953\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425056953\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e2\u05d5\u05d2\u05d9\u05d5\u05ea \u05d0\u05d5\u05e8\u05d0\u05d5","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425056962\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425056962\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05de\u05e7\u05d5\u05e4\u05dc\u05ea","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425057129\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425057129\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d5\u05e0\u05d9\u05dc \u05e7\u05dc\u05d9\u05e7","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425057136\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425057136\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e4\u05e0\u05d9\u05e0\u05ea \u05d9\u05e2\u05e8","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425057143\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425057143\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d8\u05d5\u05d5\u05d9\u05e7\u05e1","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425057472\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425057472\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e2\u05d5\u05d2\u05ea \u05d2\u05d1\u05d9\u05e0\u05d4","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425057480\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425057480\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05ea\u05d5\u05ea \u05d1\u05e9\u05de\u05e0\u05ea","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425057487\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425057487\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e4\u05e8\u05e8\u05d5 \u05e8\u05d5\u05e9\u05d4","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425058752\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425058752\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e7\u05e8\u05de\u05dc \u05de\u05dc\u05d5\u05d7","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425058759\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425058759\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d8\u05d5\u05e4\u05d9\u05e4\u05d9","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425058768\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425058768\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d1\u05d0\u05d5\u05e0\u05d8\u05d9","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425060944\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425060944\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e4\u05d9\u05e1\u05d8\u05d5\u05e7","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425060952\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425060952\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d5\u05e4\u05dc \u05de\u05d9\u05d9\u05e4\u05dc","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425060960\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425060960\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e0\u05d5\u05d8\u05dc\u05d4","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425063548\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425063548\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05dc\u05d5\u05d0\u05e7\u05e8","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425063559\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425063559\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d7\u05dc\u05d1\u05d4","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425063567\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425063567\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e7\u05e8\u05dd \u05e9\u05e0\u05d9\u05d8","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425256001\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425256001\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d1\u05e0\u05e0\u05d4 \u05e4\u05e7\u05d0\u05df","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410592\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410592\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e1\u05e0\u05d9\u05e7\u05e8\u05e1","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425256019\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425256019\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d0\u05dc\u05e4\u05d7\u05d5\u05e8\u05e1","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410136\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410136\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d8\u05d9\u05e8\u05de\u05d9\u05e1\u05d5","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410144\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410144\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05de\u05d2\u05e0\u05d5\u05dd","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410156\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410156\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e8\u05e4\u05d0\u05dc\u05d5","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410168\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410168\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d8\u05e2\u05de\u05d9","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410185\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410185\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05de\u05d9\u05dc\u05e7\u05d9","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410193\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410193\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d9\u05d5\u05d2\u05d5\u05e8\u05d8 \u05d5\u05e4\u05d9\u05e8\u05d5\u05ea \u05d9\u05e2\u05e8","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410202\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410202\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"}],"menufile":"142525634133.JPG"},"1":{"menu":"\u05d2\u05dc\u05d9\u05d3\u05d5\u05ea \u05e1\u05d5\u05e8\u05d1\u05d4","currentmenufile":"142532108933.JPG","data":[{"product":"\u05dc\u05d9\u05de\u05d5\u05df","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410247\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410247\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05dc\u05d9\u05de\u05d5\u05e0\u05e2\u05e0\u05e2","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410247\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410247\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05ea\u05d5\u05ea \u05e9\u05d3\u05d4","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410255\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410255\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e4\u05e1\u05d9\u05e4\u05dc\u05d5\u05e8\u05d4","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410270\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410270\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05de\u05e0\u05d2\u05d5","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410270\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410270\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05ea\u05e4\u05d5\u05d7 \u05d9\u05e8\u05d5\u05e7","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410270\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410270\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e4\u05d9\u05e8\u05d5\u05ea \u05d9\u05e2\u05e8","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410278\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410278\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e9\u05d5\u05e7\u05d5\u05dc\u05d3 \u05de\u05e8\u05d9\u05e8","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410284\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410284\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e7\u05d5\u05e7\u05d5\u05e1","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410290\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410290\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d3\u05d9\u05d0\u05d8 \u05e4\u05d9\u05e8\u05d5\u05ea","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"u","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410297\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410297\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"}],"menufile":"142532108933.JPG"},"2":{"menu":"\u05d2\u05dc\u05d9\u05d3\u05d5\u05ea \u05e1\u05d5\u05d9\u05d4","currentmenufile":"142532142833.JPG","data":[{"product":"\u05d1\u05d5\u05d8\u05e0\u05d9\u05dd","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410304\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410304\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d7\u05dc\u05d1\u05d4","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410310\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410310\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e4\u05d9\u05e1\u05d8\u05d5\u05e7","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410321\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410321\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05dc\u05d5\u05d8\u05d5\u05e1","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410316\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410316\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d0\u05d2\u05d5\u05d6\u05d9 \u05dc\u05d5\u05d6","desc":"","price":"84","pgraysmall":"\u05dc\u05e7"\u05d2","pgreen":"","unit":"k","pnew":"u","psale":"u","pactive":"y","currentfile":"1425410327\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425410327\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"}],"menufile":"142532142833.JPG"},"3":{"menu":"\u05e7\u05d9\u05e0\u05d5\u05d7\u05d9\u05dd","currentmenufile":"142564673833.JPG","data":[{"product":"\u05e7\u05e8\u05e4 \u05e6\u05e8\u05e4\u05ea\u05d9","desc":"","price":"18","pgraysmall":"\u05dc\u05d9\u05d7'","pgreen":"","unit":"u","pnew":"u","psale":"u","pactive":"y","currentfile":"1425646773\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425646773\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e7\u05e8\u05e4 + \u05d2\u05dc\u05d9\u05d3\u05d4 + \u05e7\u05e6\u05e4\u05ea","desc":"","price":"30","pgraysmall":"\u05dc\u05d9\u05d7'","pgreen":"","unit":"u","pnew":"u","psale":"u","pactive":"y","currentfile":"1425646783\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425646783\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05d5\u05d5\u05e4\u05dc \u05d1\u05dc\u05d2\u05d9","desc":"+ 2 \u05db\u05d3\u05d5\u05e8\u05d9\u05dd + \u05e7\u05e6\u05e4\u05ea \u05d5\u05e1\u05d9\u05e8\u05d5\u05e3","price":"49","pgraysmall":"\u05dc\u05d9\u05d7'","pgreen":"","unit":"u","pnew":"u","psale":"u","pactive":"y","currentfile":"1425646790\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425646790\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"},{"product":"\u05e1\u05d5\u05e4\u05dc\u05d4 \u05e9\u05d5\u05e7\u05d5\u05dc\u05d3 \u05d7\u05dd","desc":"+ \u05db\u05d3\u05d5\u05e8 \u05d2\u05dc\u05d9\u05d3\u05d4 + \u05e7\u05e6\u05e4\u05ea \u05d5\u05e1\u05d9\u05e8\u05d5\u05e3","price":"38","pgraysmall":"\u05dc\u05d9\u05d7'","pgreen":"","unit":"u","pnew":"u","psale":"u","pactive":"y","currentfile":"1425796952\u05ea\u05e4\u05e8\u05d9\u05d8.JPG","file":"1425796952\u05ea\u05e4\u05e8\u05d9\u05d8.JPG"}],"menufile":"142564673833.JPG"}}
Is it possible to update only 1 "product" in this way? Because if I have more then 1,000 products the update is very slow (update all the string)
Better way then get the string by SQL Select the decode,change spesific values and then update the full string?
Thanks!
To update part of a JSON string stored in MySQL, you'll indeed need to retrieve, decode to an array/object, edit, encode as JSON, and store. It's time-consuming.
I'd recommend a change to your table schema. Not sure how it's set up right now, but instead of having one table with all of your info, you could have one table just for Products, another for Orders, another for Customers, etc.
So in your Products table, you'd have one row per product:
ID | NAME | DESCRIPTION | PRICE
00123| jacket | gray wool jacket | $4.99
Then in another table when you need to reference this product, you simply need to use the ID value. You can update the table rows for products much easier than editing their JSON.
Regarding your first question, you cannot update only one product. There should be a row for each product, sounds like a terrible database design. It could make some sense if it was an static page but if you want to update or delete products it's gonna be really costly.
EDIT: Obviously when I said "you cannot update only one product" I meant to say that you must decode JSON string, modify the resulting array and encode it again to update a single product". It would have been more correct to say "you cannot update only one product without having to update the whole JSON array".

Denormalizing table afterwards query

So far I had my downloads table denormalized. I had two fields - author and country. They were separated by a space, e.g.: Jack James as for author and us uk for country.
I decided it's time to normalize it so I made a new table called downloads_authors with fields (da_id, downloads_id, da_author, da_country) and now I have:
+-----+------------+---------+----------+
|da_id|downloads_id|da_author|da_country|
+-----+------------+---------+----------+
|1 |1 |Jack |us |
+-----+------------+---------+----------+
|2 |1 |James |uk |
+-----+------------+---------+----------+
So far so good.. but in the way I used to have them, I used explode and with a very bad function, I was getting the desired result - <flag img> Jack, <flag img> James
Now, when I have them in another table I cannot think of a way to do this:
SELECT * FROM downloads and list the respective author(s) without having an inner loop (because if I do a JOIN then I will have the information from downloads again and again).
Desired otuput is:
item
- author
item
- author
- author
Am I wrong about the JOIN and is it the way to go?
Your options are join and use the download information the first time you get a new download id and ignore the download information until you get a new download id. Or do what I said, query the data out and loop to build a new array. Or you could also use group_concat to join the authors together back into a single string.
I would just query out all downloads then query out all authors. Loop over the downloads and assign them to an array where the download id is the key. Then loop over the authors and assign the different authors to a sub-array of the downloads using the download id.

Have an array in a SQL field. How to display it systematically?

I have a field in a data feed coming in with some values separated by commas. For one record, the values are:
A06,C05,C06,C15,C18,C19,C21,C22,E05,E22,G11,J02,J07,L04,L07,M01,M05,N03,N07,N10,N11,N12,N18,N19,N20,N24,O02,O03,O04,O06,O09,O14,O15,O16,O20,O21,O31,Q01,Q04,Q08,R07,S08,T08,T12,T23,T32,U01,U03,U04,U06,U13,W09,W11,W16,W19,W30,W45,X02,X03,X12,Z07
I have a separate table with some descriptions as to what each code means. When I query the main table and get this field name as a value, I can use explode to get it into an array and use a foreach loop to output each value.
The problem is, I want to display the description stored in another table. What's the proper way of iterating through this to display these values in a list?
As an example, C21 means "Gated Community."
You can use FIND_IN_SET() function for that.
Example you have record like this
Orders Table
------------------------------------
OrderID | attachedCompanyIDs
------------------------------------
1 1,2,3 -- comma separated values
2 2,4
and
Company Table
--------------------------------------
CompanyID | name
--------------------------------------
1 Company 1
2 Another Company
3 StackOverflow
4 Nothing
Using the function
SELECT name
FROM orders, company
WHERE orderID = 1 AND FIND_IN_SET(companyID, attachedCompanyIDs)
will result
name
---------------
Company 1
Another Company
StackOverflow
As you have tagged codeigniter you could use the built in Active Record's method $this->db->where_in(); to get the description. For example consider the code below
$codes = array('A06', 'C05', 'C06');
$this->db->where_in('description', $codes);
// Produces: WHERE codes IN ('A06', 'C05', 'C06')
For more information about Active Records of Codeigniter refer Active Record Class
For more information on how mysql WHERE IN works refer Tutorial
Just for follow the books, the best way of doing this in SQL language is to use the relationship.
For understand this I recommend you read this simple paper http://net.tutsplus.com/tutorials/databases/sql-for-beginners-part-3-database-relationships/
and maybe this http://www.informit.com/articles/article.aspx?p=30875&seqNum=5 for SELECT the data, or search for yourself on the web, and I recommend you to try yourself the examples. SQL Relationship Is good and necessary for security and many others reasons.

Wordpress MySQL data retrieval Hotel booking reservation system

I am trying to create a Hotel reservation system using wordpress. Now, I know all the queries for data retrieval in a non-wordpress mode but I am facing some data-organization issues in dealing with wordpress. My hotels details are stored in the posts table and as custom fields for the hotel. For rates of the hotels, I created a different table according where rates vary according to months. I'd like the data to be displayed like this in my search results page:
Hotel_1 Name: (Will come from Post Name)
Hotel_1 Details: (Will come from custom fields)
Hotel_1 description: (will come from excerpt)
Room_1 Name for Hotel_1: Total Rates for selected dates
Room_2 Name for Hotel_1: Total Rates for selected dates
----------------------------------------------------------
Hotel_2 Name: (Will come from Post Name)
Hotel_2 Details: (Will come from custom fields)
Hotel_2 description: (will come from excerpt)
Room_1 Name for Hotel_2: Total Rates for selected dates
Room_2 Name for Hotel_2: Total Rates for selected dates
----------------------------------------------------------
and so on and so forth.....
----------------------------------------------------------
Pagination>>
----------------------------------------------------------
My rates table looks like this
mysql> select * from rates;
+-------------+---------+---------+-------------------+------------+------------
+-----------+--------------+-----------+------------------------------------+
| primary_key | post_id | room_id | room_type | start_date | end_date
| adultRate | extraBedRate | childRate | inclusions |
+-------------+---------+---------+-------------------+------------+------------
+-----------+--------------+-----------+------------------------------------+
where post_id = ID of the post in wp_posts table
room_id = ID given to a room
room_type = Name of the room
1 post (post_id) may have several room_types.
Any help is much appreciated. Thanks.
I just need help on
a. how to organize or create relationships among table to get desired result, &
b. how to get them displayed in wordpress. Some classes or functions need to be considered.
I am very very new to php and mysql and this is my first assignment. My skill level is such that I can make changes to a code but right now it is very difficult for me to write from scratch. However with the help around here I intend to learn.
thanks
You're approaching this problem from the wrong point of view. There's a better way to handle this kind of information on WordPress and it doesn't (necessarily) involve creating new tables. WordPress handles CRUD natively so you will use pre-made functions instead of writing it from scratch.
Also, forget about using the original posts structure to do this, it can be a major headache. Instead, read everything you can about Custom Post Types and specially this awesome tutorial from Justin Tadlock

How to store an array of an inventory in php/mysql?

The end user has an inventory such as:
dogs:0, cats:3, dragons:20 etc you get the point.
So for a 30 inventory string array, what is the best way to store the values in mysql
and then use them with php? do i need to use json for this? because i would like
to update for instance a user "buying" another dragon and update the record occasionally.
Just use a seperate table for this, which has a reference to the object (if an object table exist), the player it belongs to and the number of items. No need to store a json string in a table.
Table Objects
-----------------
id | Object name
Table Inventory
----------------------------------
id | player_id | object_id | count
Store in a table containing Id, Name, Quantity.
You need JSON to communicate with php without reloading the page (AJAX), but it can be generated dynamically.

Categories