I am developing a bespoke pricing matrix for a client, they have codes to distinguish products with different options, there are 6 different variations for one code, as there are 6 different types of material that incur different costs, other bolt ons also change the code and the costs based on the material... and so on.
I'll start by showing you my prices database structure (MySQL)
|------
|Field|Type|Null|Default
|------
|//**id**//|mediumint(9)|No|
|jp_code|varchar(7)|No|
|brand_rate|text|No|
|price_25|text|No|
|price_50|text|No|
|price_100|text|No|
|price_250|text|No|
|price_500|text|No|
|price_1000|text|No|
I store all 6 prices for each code as JSON in the prices and brand rate field, this is processed by JS later on.
So a typical entry for that database looks like this:
|1|JP6000|["F","F","n\/a","F","F","F"]|["2.92","2.92","n\/a","4.86","6.35","7.62"]|["2.77","2.77","n\/a","4.62","6.03","7.24"]|["2.55","2.55","4.21","4.25","5.55","6.66"]|["2.45","2.45","3.83","4.08","5.33","6.40"]|["2.38","2.38","3.64","3.96","5.17","6.20"]|["2.50","2.33","3.47","3.89","5.08","6.10"]
The client needs to be able to upload a CSV, which can seamlessly update these prices.
The CSV file will look like this:
If you take a look at the MySQL row entry you should be able to marry up the data, so onto the problem!
I am using this to work with CSV files: parseCSV v0.4.3 beta http://code.google.com/p/parsecsv-for-php/
I have got this to group all the results by the JP Code like this:
array(2) {
["JP6000"]=>
array(6) {
[0]=>
array(9) {
["Code"]=>
string(6) "JP6000"
["Brand Rate"]=>
string(1) "F"
["Price_25"]=>
string(4) "2.92"
["Price_50"]=>
string(4) "2.77"
["Price_100"]=>
string(4) "2.55"
["Price_250"]=>
string(4) "2.45"
["Price_500"]=>
string(4) "2.38"
["Price_1000"]=>
string(4) "2.33"
["Material"]=>
string(10) "Belluno PU"
}
[1]=>
array(9) {
["Code"]=>
string(6) "JP6000"
["Brand Rate"]=>
string(1) "F"
["Price_25"]=>
string(4) "2.92"
["Price_50"]=>
string(4) "2.77"
["Price_100"]=>
string(4) "2.55"
["Price_250"]=>
string(4) "2.45"
["Price_500"]=>
string(4) "2.38"
["Price_1000"]=>
string(4) "2.33"
["Material"]=>
string(9) "Torino PU"
}
[2]=>
array(9) {
["Code"]=>
string(6) "JP6000"
["Brand Rate"]=>
string(3) "n/a"
["Price_25"]=>
string(3) "n/a"
["Price_50"]=>
string(3) "n/a"
["Price_100"]=>
string(4) "4.21"
["Price_250"]=>
string(4) "3.83"
["Price_500"]=>
string(4) "3.64"
["Price_1000"]=>
string(4) "3.47"
["Material"]=>
string(11) "Full Colour"
}
[3]=>
array(9) {
["Code"]=>
string(6) "JP6000"
["Brand Rate"]=>
string(1) "F"
["Price_25"]=>
string(4) "4.86"
["Price_50"]=>
string(4) "4.62"
["Price_100"]=>
string(4) "4.25"
["Price_250"]=>
string(4) "4.08"
["Price_500"]=>
string(4) "3.96"
["Price_1000"]=>
string(4) "3.89"
["Material"]=>
string(8) "Finecell"
}
[4]=>
array(9) {
["Code"]=>
string(6) "JP6000"
["Brand Rate"]=>
string(1) "F"
["Price_25"]=>
string(4) "6.35"
["Price_50"]=>
string(4) "6.03"
["Price_100"]=>
string(4) "5.55"
["Price_250"]=>
string(4) "5.33"
["Price_500"]=>
string(4) "5.17"
["Price_1000"]=>
string(4) "5.08"
["Material"]=>
string(5) "Nappa"
}
[5]=>
array(9) {
["Code"]=>
string(6) "JP6000"
["Brand Rate"]=>
string(1) "F"
["Price_25"]=>
string(4) "7.62"
["Price_50"]=>
string(4) "7.24"
["Price_100"]=>
string(4) "6.66"
["Price_250"]=>
string(3) "6.4"
["Price_500"]=>
string(3) "6.2"
["Price_1000"]=>
string(3) "6.1"
["Material"]=>
string(8) "Richmond"
}
}
["JP6010"]=>
array(6) {
[0]=>
array(9) {
["Code"]=>
string(6) "JP6010"
["Brand Rate"]=>
string(1) "F"
["Price_25"]=>
string(4) "3.41"
["Price_50"]=>
string(4) "3.24"
["Price_100"]=>
string(4) "2.98"
["Price_250"]=>
string(4) "2.86"
["Price_500"]=>
string(4) "2.78"
["Price_1000"]=>
string(4) "2.73"
["Material"]=>
string(10) "Belluno PU"
}
[1]=>
array(9) {
["Code"]=>
string(6) "JP6010"
["Brand Rate"]=>
string(1) "F"
["Price_25"]=>
string(4) "3.41"
["Price_50"]=>
string(4) "3.24"
["Price_100"]=>
string(4) "2.98"
["Price_250"]=>
string(4) "2.86"
["Price_500"]=>
string(4) "2.78"
["Price_1000"]=>
string(4) "2.73"
["Material"]=>
string(9) "Torino PU"
}
[2]=>
array(9) {
["Code"]=>
string(6) "JP6010"
["Brand Rate"]=>
string(3) "n/a"
["Price_25"]=>
string(3) "n/a"
["Price_50"]=>
string(3) "n/a"
["Price_100"]=>
string(4) "4.77"
["Price_250"]=>
string(4) "4.33"
["Price_500"]=>
string(4) "4.13"
["Price_1000"]=>
string(4) "3.93"
["Material"]=>
string(11) "Full Colour"
}
[3]=>
array(9) {
["Code"]=>
string(6) "JP6010"
["Brand Rate"]=>
string(1) "F"
["Price_25"]=>
string(4) "5.41"
["Price_50"]=>
string(4) "5.14"
["Price_100"]=>
string(4) "4.74"
["Price_250"]=>
string(4) "4.55"
["Price_500"]=>
string(4) "4.41"
["Price_1000"]=>
string(4) "4.33"
["Material"]=>
string(8) "Finecell"
}
[4]=>
array(9) {
["Code"]=>
string(6) "JP6010"
["Brand Rate"]=>
string(1) "F"
["Price_25"]=>
string(3) "6.9"
["Price_50"]=>
string(4) "6.56"
["Price_100"]=>
string(4) "6.04"
["Price_250"]=>
string(3) "5.8"
["Price_500"]=>
string(4) "5.63"
["Price_1000"]=>
string(4) "5.52"
["Material"]=>
string(5) "Nappa"
}
[5]=>
array(9) {
["Code"]=>
string(6) "JP6010"
["Brand Rate"]=>
string(1) "F"
["Price_25"]=>
string(4) "8.28"
["Price_50"]=>
string(4) "7.87"
["Price_100"]=>
string(4) "7.25"
["Price_250"]=>
string(4) "6.96"
["Price_500"]=>
string(4) "6.76"
["Price_1000"]=>
string(4) "6.62"
["Material"]=>
string(8) "Richmond"
}
}
}
array(2) {
["JP6000"]=>
array(6) {
[0]=>
array(9) {
["Code"]=>
string(6) "JP6000"
["Brand Rate"]=>
string(1) "F"
["Price_25"]=>
string(4) "2.92"
["Price_50"]=>
string(4) "2.77"
["Price_100"]=>
string(4) "2.55"
["Price_250"]=>
string(4) "2.45"
["Price_500"]=>
string(4) "2.38"
["Price_1000"]=>
string(4) "2.33"
["Material"]=>
string(10) "Belluno PU"
}
[1]=>
array(9) {
["Code"]=>
string(6) "JP6000"
["Brand Rate"]=>
string(1) "F"
["Price_25"]=>
string(4) "2.92"
["Price_50"]=>
string(4) "2.77"
["Price_100"]=>
string(4) "2.55"
["Price_250"]=>
string(4) "2.45"
["Price_500"]=>
string(4) "2.38"
["Price_1000"]=>
string(4) "2.33"
["Material"]=>
string(9) "Torino PU"
}
[2]=>
array(9) {
["Code"]=>
string(6) "JP6000"
["Brand Rate"]=>
string(3) "n/a"
["Price_25"]=>
string(3) "n/a"
["Price_50"]=>
string(3) "n/a"
["Price_100"]=>
string(4) "4.21"
["Price_250"]=>
string(4) "3.83"
["Price_500"]=>
string(4) "3.64"
["Price_1000"]=>
string(4) "3.47"
["Material"]=>
string(11) "Full Colour"
}
[3]=>
array(9) {
["Code"]=>
string(6) "JP6000"
["Brand Rate"]=>
string(1) "F"
["Price_25"]=>
string(4) "4.86"
["Price_50"]=>
string(4) "4.62"
["Price_100"]=>
string(4) "4.25"
["Price_250"]=>
string(4) "4.08"
["Price_500"]=>
string(4) "3.96"
["Price_1000"]=>
string(4) "3.89"
["Material"]=>
string(8) "Finecell"
}
[4]=>
array(9) {
["Code"]=>
string(6) "JP6000"
["Brand Rate"]=>
string(1) "F"
["Price_25"]=>
string(4) "6.35"
["Price_50"]=>
string(4) "6.03"
["Price_100"]=>
string(4) "5.55"
["Price_250"]=>
string(4) "5.33"
["Price_500"]=>
string(4) "5.17"
["Price_1000"]=>
string(4) "5.08"
["Material"]=>
string(5) "Nappa"
}
[5]=>
array(9) {
["Code"]=>
string(6) "JP6000"
["Brand Rate"]=>
string(1) "F"
["Price_25"]=>
string(4) "7.62"
["Price_50"]=>
string(4) "7.24"
["Price_100"]=>
string(4) "6.66"
["Price_250"]=>
string(3) "6.4"
["Price_500"]=>
string(3) "6.2"
["Price_1000"]=>
string(3) "6.1"
["Material"]=>
string(8) "Richmond"
}
}
["JP6010"]=>
array(6) {
[0]=>
array(9) {
["Code"]=>
string(6) "JP6010"
["Brand Rate"]=>
string(1) "F"
["Price_25"]=>
string(4) "3.41"
["Price_50"]=>
string(4) "3.24"
["Price_100"]=>
string(4) "2.98"
["Price_250"]=>
string(4) "2.86"
["Price_500"]=>
string(4) "2.78"
["Price_1000"]=>
string(4) "2.73"
["Material"]=>
string(10) "Belluno PU"
}
[1]=>
array(9) {
["Code"]=>
string(6) "JP6010"
["Brand Rate"]=>
string(1) "F"
["Price_25"]=>
string(4) "3.41"
["Price_50"]=>
string(4) "3.24"
["Price_100"]=>
string(4) "2.98"
["Price_250"]=>
string(4) "2.86"
["Price_500"]=>
string(4) "2.78"
["Price_1000"]=>
string(4) "2.73"
["Material"]=>
string(9) "Torino PU"
}
[2]=>
array(9) {
["Code"]=>
string(6) "JP6010"
["Brand Rate"]=>
string(3) "n/a"
["Price_25"]=>
string(3) "n/a"
["Price_50"]=>
string(3) "n/a"
["Price_100"]=>
string(4) "4.77"
["Price_250"]=>
string(4) "4.33"
["Price_500"]=>
string(4) "4.13"
["Price_1000"]=>
string(4) "3.93"
["Material"]=>
string(11) "Full Colour"
}
[3]=>
array(9) {
["Code"]=>
string(6) "JP6010"
["Brand Rate"]=>
string(1) "F"
["Price_25"]=>
string(4) "5.41"
["Price_50"]=>
string(4) "5.14"
["Price_100"]=>
string(4) "4.74"
["Price_250"]=>
string(4) "4.55"
["Price_500"]=>
string(4) "4.41"
["Price_1000"]=>
string(4) "4.33"
["Material"]=>
string(8) "Finecell"
}
[4]=>
array(9) {
["Code"]=>
string(6) "JP6010"
["Brand Rate"]=>
string(1) "F"
["Price_25"]=>
string(3) "6.9"
["Price_50"]=>
string(4) "6.56"
["Price_100"]=>
string(4) "6.04"
["Price_250"]=>
string(3) "5.8"
["Price_500"]=>
string(4) "5.63"
["Price_1000"]=>
string(4) "5.52"
["Material"]=>
string(5) "Nappa"
}
[5]=>
array(9) {
["Code"]=>
string(6) "JP6010"
["Brand Rate"]=>
string(1) "F"
["Price_25"]=>
string(4) "8.28"
["Price_50"]=>
string(4) "7.87"
["Price_100"]=>
string(4) "7.25"
["Price_250"]=>
string(4) "6.96"
["Price_500"]=>
string(4) "6.76"
["Price_1000"]=>
string(4) "6.62"
["Material"]=>
string(8) "Richmond"
}
}
}
So what I need to do is further group this data and convert it into a JSON string like you see in the MySQL records using the information for each code.
The first entry for price_25 json array for JP6000 would be 2.92 and the second 2.92 - third being n/a
This also needs to loop through and update each mysql row accordingly based on the JP Code being the primary key.
I hope this all makes sense? very tricky!