Finding items in multiple arrays - php

Hello I am building a eCommerce web application using Laravel and for my products there are three types of filter for example: Color, Style, Industry and these each filter contains some values like this:
color => red, green, blue yellow....
Style => Bold,Conservative,Elegant,Floral...
Industry => Agriculture & Farming, Animals & Pet Care,Art & Entertainment...
and all products have all three filters so each product have some combination of each filter like Product_1 =>blue,Floral,Agriculture & Farming. I am not inserting filters in my product table instead of that I have created a list of all possible combination with an id of each combination so there are like total 4000 combinations of all three types filters so in my product table in just using a column like filter_ID which holds the ID of combination.
Now when I am showing products I need to count items available for each filter like Agriculture & Farming [ Total Numbers of product with this filler].
And in that combination list I am not using real filter name instead of that I given an Integer ID to each filter like:
Agriculture & Farming = 1, Animals & Pet Care = 2, blue = 3....
So now I have three things to counts available products in each filter.
Combination list
Filter_IDs from Product table
Integer ID of each
Fillter
I tried to count using loops, but its not returning correct value my sample data is large so I can not insert here I am including link to my php file where you can find all data and also what results I need.Link to php file
Code:
// This is all three fillters possible combination in this array 1st level array is a combination
//And in 2nd level array it conatins 4 items which are:
//1st item is Combination ID and
//2nd item is Interger value of actual Fillter (Industry)
//3rd item is Interger value of actual Fillter (Style)
//4th item is Interger value of actual Fillter (color)
// Using this for faster performance
$filltersCombination_integer = [[1,1,14,28],[2,1,14,29],[3,1,14,30],[4,1,14,31],[5,1,14,32],[6,1,14,33],[7,1,14,34],[8,1,14,35],[9,1,14,36],[10,1,14,37],[11,1,14,38],[12,1,14,39],[13,1,14,40],[14,1,14,41],[15,1,14,42],[16,1,14,43],[17,1,14,44],[18,1,14,45],[19,1,14,46],[20,1,15,28],[21,1,15,29],[22,1,15,30],[23,1,15,31],[24,1,15,32],[25,1,15,33],[26,1,15,34],[27,1,15,35],[28,1,15,36],[29,1,15,37],[30,1,15,38],[31,1,15,39],[32,1,15,40],[33,1,15,41],[34,1,15,42],[35,1,15,43],[36,1,15,44],[37,1,15,45],[38,1,15,46],[39,1,16,28],[40,1,16,29],[41,1,16,30],[42,1,16,31],[43,1,16,32],[44,1,16,33],[45,1,16,34],[46,1,16,35],[47,1,16,36],[48,1,16,37],[49,1,16,38],[50,1,16,39],[51,1,16,40],[52,1,16,41],[53,1,16,42],[54,1,16,43],[55,1,16,44],[56,1,16,45],[57,1,16,46],[58,1,17,28],[59,1,17,29],[60,1,17,30],[61,1,17,31],[62,1,17,32],[63,1,17,33],[64,1,17,34],[65,1,17,35],[66,1,17,36],[67,1,17,37],[68,1,17,38],[69,1,17,39],[70,1,17,40],[71,1,17,41],[72,1,17,42],[73,1,17,43],[74,1,17,44],[75,1,17,45],[76,1,17,46],[77,1,18,28],[78,1,18,29],[79,1,18,30],[80,1,18,31],[81,1,18,32],[82,1,18,33],[83,1,18,34],[84,1,18,35],[85,1,18,36],[86,1,18,37],[87,1,18,38],[88,1,18,39],[89,1,18,40],[90,1,18,41],[91,1,18,42],[92,1,18,43],[93,1,18,44],[94,1,18,45],[95,1,18,46],[96,1,19,28],[97,1,19,29],[98,1,19,30],[99,1,19,31],[100,1,19,32],[101,1,19,33],[102,1,19,34],[103,1,19,35],[104,1,19,36],[105,1,19,37],[106,1,19,38],[107,1,19,39],[108,1,19,40],[109,1,19,41],[110,1,19,42],[111,1,19,43],[112,1,19,44],[113,1,19,45],[114,1,19,46],[115,1,20,28],[116,1,20,29],[117,1,20,30],[118,1,20,31],[119,1,20,32],[120,1,20,33],[121,1,20,34],[122,1,20,35],[123,1,20,36],[124,1,20,37],[125,1,20,38],[126,1,20,39],[127,1,20,40],[128,1,20,41],[129,1,20,42],[130,1,20,43],[131,1,20,44],[132,1,20,45],[133,1,20,46],[134,1,21,28],[135,1,21,29],[136,1,21,30],[137,1,21,31],[138,1,21,32],[139,1,21,33],[140,1,21,34],[141,1,21,35],[142,1,21,36],[143,1,21,37],[144,1,21,38],[145,1,21,39],[146,1,21,40],[147,1,21,41],[148,1,21,42],[149,1,21,43],[150,1,21,44],[151,1,21,45],[152,1,21,46],[153,1,22,28],[154,1,22,29],[155,1,22,30],[156,1,22,31],[157,1,22,32],[158,1,22,33],[159,1,22,34],[160,1,22,35],[161,1,22,36],[162,1,22,37],[163,1,22,38],[164,1,22,39],[165,1,22,40],[166,1,22,41],[167,1,22,42],[168,1,22,43],[169,1,22,44],[170,1,22,45],[171,1,22,46],[172,1,23,28],[173,1,23,29],[174,1,23,30],[175,1,23,31],[176,1,23,32],[177,1,23,33],[178,1,23,34],[179,1,23,35],[180,1,23,36],[181,1,23,37],[182,1,23,38],[183,1,23,39],[184,1,23,40],[185,1,23,41],[186,1,23,42],[187,1,23,43],[188,1,23,44],[189,1,23,45],[190,1,23,46],[191,1,24,28],[192,1,24,29],[193,1,24,30],[194,1,24,31],[195,1,24,32],[196,1,24,33],[197,1,24,34],[198,1,24,35],[199,1,24,36],[200,1,24,37],[201,1,24,38],[202,1,24,39],[203,1,24,40],[204,1,24,41],[205,1,24,42],[206,1,24,43],[207,1,24,44],[208,1,24,45],[209,1,24,46],[210,1,25,28],[211,1,25,29],[212,1,25,30],[213,1,25,31],[214,1,25,32],[215,1,25,33],[216,1,25,34],[217,1,25,35],[218,1,25,36],[219,1,25,37],[220,1,25,38],[221,1,25,39],[222,1,25,40],[223,1,25,41],[224,1,25,42],[225,1,25,43],[226,1,25,44],[227,1,25,45],[228,1,25,46],[229,1,26,28],[230,1,26,29],[231,1,26,30],[232,1,26,31],[233,1,26,32],[234,1,26,33],[235,1,26,34],[236,1,26,35],[237,1,26,36],[238,1,26,37],[239,1,26,38],[240,1,26,39],[241,1,26,40],[242,1,26,41],[243,1,26,42],[244,1,26,43],[245,1,26,44],[246,1,26,45],[247,1,26,46],[248,1,27,28],[249,1,27,29],[250,1,27,30],[251,1,27,31],[252,1,27,32],[253,1,27,33],[254,1,27,34],[255,1,27,35],[256,1,27,36],[257,1,27,37],[258,1,27,38],[259,1,27,39],[260,1,27,40],[261,1,27,41],[262,1,27,42],[263,1,27,43],[264,1,27,44],[265,1,27,45],[266,1,27,46],[267,2,14,28],[268,2,14,29],[269,2,14,30],[270,2,14,31],[271,2,14,32],[272,2,14,33],[273,2,14,34],[274,2,14,35],[275,2,14,36],[276,2,14,37],[277,2,14,38],[278,2,14,39],[279,2,14,40],[280,2,14,41],[281,2,14,42],[282,2,14,43],[283,2,14,44],[284,2,14,45],[285,2,14,46],[286,2,15,28],[287,2,15,29],[288,2,15,30],[289,2,15,31],[290,2,15,32],[291,2,15,33],[292,2,15,34],[293,2,15,35],[294,2,15,36],[295,2,15,37],[296,2,15,38],[297,2,15,39],[298,2,15,40],[299,2,15,41],[300,2,15,42],[301,2,15,43],[302,2,15,44],[303,2,15,45],[304,2,15,46],[305,2,16,28],[306,2,16,29],[307,2,16,30],[308,2,16,31],[309,2,16,32],[310,2,16,33],[311,2,16,34],[312,2,16,35],[313,2,16,36],[314,2,16,37],[315,2,16,38],[316,2,16,39],[317,2,16,40],[318,2,16,41],[319,2,16,42],[320,2,16,43],[321,2,16,44],[322,2,16,45],[323,2,16,46],[324,2,17,28],[325,2,17,29],[326,2,17,30],[327,2,17,31],[328,2,17,32],[329,2,17,33],[330,2,17,34],[331,2,17,35],[332,2,17,36],[333,2,17,37],[334,2,17,38],[335,2,17,39],[336,2,17,40],[337,2,17,41],[338,2,17,42],[339,2,17,43],[340,2,17,44],[341,2,17,45],[342,2,17,46],[343,2,18,28],[344,2,18,29],[345,2,18,30],[346,2,18,31],[347,2,18,32],[348,2,18,33],[349,2,18,34],[350,2,18,35],[351,2,18,36],[352,2,18,37],[353,2,18,38],[354,2,18,39],[355,2,18,40],[356,2,18,41],[357,2,18,42],[358,2,18,43],[359,2,18,44],[360,2,18,45],[361,2,18,46],[362,2,19,28],[363,2,19,29],[364,2,19,30],[365,2,19,31],[366,2,19,32],[367,2,19,33],[368,2,19,34],[369,2,19,35],[370,2,19,36],[371,2,19,37],[372,2,19,38],[373,2,19,39],[374,2,19,40],[375,2,19,41],[376,2,19,42],[377,2,19,43],[378,2,19,44],[379,2,19,45],[380,2,19,46],[381,2,20,28],[382,2,20,29],[383,2,20,30],[384,2,20,31],[385,2,20,32],[386,2,20,33],[387,2,20,34],[388,2,20,35],[389,2,20,36],[390,2,20,37],[391,2,20,38],[392,2,20,39],[393,2,20,40],[394,2,20,41],[395,2,20,42],[396,2,20,43],[397,2,20,44],[398,2,20,45],[399,2,20,46],[400,2,21,28],[401,2,21,29],[402,2,21,30],[403,2,21,31],[404,2,21,32],[405,2,21,33],[406,2,21,34],[407,2,21,35],[408,2,21,36],[409,2,21,37],[410,2,21,38],[411,2,21,39],[412,2,21,40],[413,2,21,41],[414,2,21,42],[415,2,21,43],[416,2,21,44],[417,2,21,45],[418,2,21,46],[419,2,22,28],[420,2,22,29],[421,2,22,30],[422,2,22,31],[423,2,22,32],[424,2,22,33],[425,2,22,34],[426,2,22,35],[427,2,22,36],[428,2,22,37],[429,2,22,38],[430,2,22,39],[431,2,22,40],[432,2,22,41],[433,2,22,42],[434,2,22,43],[435,2,22,44],[436,2,22,45],[437,2,22,46],[438,2,23,28],[439,2,23,29],[440,2,23,30],[441,2,23,31],[442,2,23,32],[443,2,23,33],[444,2,23,34],[445,2,23,35],[446,2,23,36],[447,2,23,37],[448,2,23,38],[449,2,23,39],[450,2,23,40],[451,2,23,41],[452,2,23,42],[453,2,23,43],[454,2,23,44],[455,2,23,45],[456,2,23,46],[457,2,24,28],[458,2,24,29],[459,2,24,30],[460,2,24,31],[461,2,24,32],[462,2,24,33],[463,2,24,34],[464,2,24,35],[465,2,24,36],[466,2,24,37],[467,2,24,38],[468,2,24,39],[469,2,24,40],[470,2,24,41],[471,2,24,42],[472,2,24,43],[473,2,24,44],[474,2,24,45],[475,2,24,46],[476,2,25,28],[477,2,25,29],[478,2,25,30],[479,2,25,31],[480,2,25,32],[481,2,25,33],[482,2,25,34],[483,2,25,35],[484,2,25,36],[485,2,25,37],[486,2,25,38],[487,2,25,39],[488,2,25,40],[489,2,25,41],[490,2,25,42],[491,2,25,43],[492,2,25,44],[493,2,25,45],[494,2,25,46],[495,2,26,28],[496,2,26,29],[497,2,26,30],[498,2,26,31],[499,2,26,32],[500,2,26,33],[501,2,26,34],[502,2,26,35],[503,2,26,36],[504,2,26,37],[505,2,26,38],[506,2,26,39],[507,2,26,40],[508,2,26,41],[509,2,26,42],[510,2,26,43],[511,2,26,44],[512,2,26,45],[513,2,26,46],[514,2,27,28],[515,2,27,29],[516,2,27,30],[517,2,27,31],[518,2,27,32],[519,2,27,33],[520,2,27,34],[521,2,27,35],[522,2,27,36],[523,2,27,37],[524,2,27,38],[525,2,27,39],[526,2,27,40],[527,2,27,41],[528,2,27,42],[529,2,27,43],[530,2,27,44],[531,2,27,45],[532,2,27,46],[533,3,14,28],[534,3,14,29],[535,3,14,30],[536,3,14,31],[537,3,14,32],[538,3,14,33],[539,3,14,34],[540,3,14,35],[541,3,14,36],[542,3,14,37],[543,3,14,38],[544,3,14,39],[545,3,14,40],[546,3,14,41],[547,3,14,42],[548,3,14,43],[549,3,14,44],[550,3,14,45],[551,3,14,46],[552,3,15,28],[553,3,15,29],[554,3,15,30],[555,3,15,31],[556,3,15,32],[557,3,15,33],[558,3,15,34],[559,3,15,35],[560,3,15,36],[561,3,15,37],[562,3,15,38],[563,3,15,39],[564,3,15,40],[565,3,15,41],[566,3,15,42],[567,3,15,43],[568,3,15,44],[569,3,15,45],[570,3,15,46],[571,3,16,28],[572,3,16,29],[573,3,16,30],[574,3,16,31],[575,3,16,32],[576,3,16,33],[577,3,16,34],[578,3,16,35],[579,3,16,36],[580,3,16,37],[581,3,16,38],[582,3,16,39],[583,3,16,40],[584,3,16,41],[585,3,16,42],[586,3,16,43],[587,3,16,44],[588,3,16,45],[589,3,16,46],[590,3,17,28],[591,3,17,29],[592,3,17,30],[593,3,17,31],[594,3,17,32],[595,3,17,33],[596,3,17,34],[597,3,17,35],[598,3,17,36],[599,3,17,37],[600,3,17,38],[601,3,17,39],[602,3,17,40],[603,3,17,41],[604,3,17,42],[605,3,17,43],[606,3,17,44],[607,3,17,45],[608,3,17,46],[609,3,18,28],[610,3,18,29],[611,3,18,30],[612,3,18,31],[613,3,18,32],[614,3,18,33],[615,3,18,34],[616,3,18,35],[617,3,18,36],[618,3,18,37],[619,3,18,38],[620,3,18,39],[621,3,18,40],[622,3,18,41],[623,3,18,42],[624,3,18,43],[625,3,18,44],[626,3,18,45],[627,3,18,46],[628,3,19,28],[629,3,19,29],[630,3,19,30],[631,3,19,31],[632,3,19,32],[633,3,19,33],[634,3,19,34],[635,3,19,35],[636,3,19,36],[637,3,19,37],[638,3,19,38],[639,3,19,39],[640,3,19,40],[641,3,19,41],[642,3,19,42],[643,3,19,43],[644,3,19,44],[645,3,19,45],[646,3,19,46],[647,3,20,28],[648,3,20,29],[649,3,20,30],[650,3,20,31],[651,3,20,32],[652,3,20,33],[653,3,20,34],[654,3,20,35],[655,3,20,36],[656,3,20,37],[657,3,20,38],[658,3,20,39],[659,3,20,40],[660,3,20,41],[661,3,20,42],[662,3,20,43],[663,3,20,44],[664,3,20,45],[665,3,20,46],[666,3,21,28],[667,3,21,29],[668,3,21,30],[669,3,21,31],[670,3,21,32],[671,3,21,33],[672,3,21,34],[673,3,21,35],[674,3,21,36],[675,3,21,37],[676,3,21,38],[677,3,21,39],[678,3,21,40],[679,3,21,41],[680,3,21,42],[681,3,21,43],[682,3,21,44],[683,3,21,45],[684,3,21,46],[685,3,22,28],[686,3,22,29],[687,3,22,30],[688,3,22,31],[689,3,22,32],[690,3,22,33],[691,3,22,34],[692,3,22,35],[693,3,22,36],[694,3,22,37],[695,3,22,38],[696,3,22,39],[697,3,22,40],[698,3,22,41],[699,3,22,42],[700,3,22,43],[701,3,22,44],[702,3,22,45],[703,3,22,46],[704,3,23,28],[705,3,23,29],[706,3,23,30],[707,3,23,31],[708,3,23,32],[709,3,23,33],[710,3,23,34],[711,3,23,35],[712,3,23,36],[713,3,23,37],[714,3,23,38],[715,3,23,39],[716,3,23,40],[717,3,23,41],[718,3,23,42],[719,3,23,43],[720,3,23,44],[721,3,23,45],[722,3,23,46],[723,3,24,28],[724,3,24,29],[725,3,24,30],[726,3,24,31],[727,3,24,32],[728,3,24,33],[729,3,24,34],[730,3,24,35],[731,3,24,36],[732,3,24,37],[733,3,24,38],[734,3,24,39],[735,3,24,40],[736,3,24,41],[737,3,24,42],[738,3,24,43],[739,3,24,44],[740,3,24,45],[741,3,24,46],[742,3,25,28],[743,3,25,29],[744,3,25,30],[745,3,25,31],[746,3,25,32],[747,3,25,33],[748,3,25,34],[749,3,25,35],[750,3,25,36],[751,3,25,37],[752,3,25,38],[753,3,25,39],[754,3,25,40],[755,3,25,41],[756,3,25,42],[757,3,25,43],[758,3,25,44],[759,3,25,45],[760,3,25,46],[761,3,26,28],[762,3,26,29],[763,3,26,30],[764,3,26,31],[765,3,26,32],[766,3,26,33],[767,3,26,34],[768,3,26,35],[769,3,26,36],[770,3,26,37],[771,3,26,38],[772,3,26,39],[773,3,26,40],[774,3,26,41],[775,3,26,42],[776,3,26,43],[777,3,26,44],[778,3,26,45],[779,3,26,46],[780,3,27,28],[781,3,27,29],[782,3,27,30],[783,3,27,31],[784,3,27,32],[785,3,27,33],[786,3,27,34],[787,3,27,35],[788,3,27,36],[789,3,27,37],[790,3,27,38],[791,3,27,39],[792,3,27,40],[793,3,27,41],[794,3,27,42],[795,3,27,43],[796,3,27,44],[797,3,27,45],[798,3,27,46],[799,4,14,28],[800,4,14,29],[801,4,14,30],[802,4,14,31],[803,4,14,32],[804,4,14,33],[805,4,14,34],[806,4,14,35],[807,4,14,36],[808,4,14,37],[809,4,14,38],[810,4,14,39],[811,4,14,40],[812,4,14,41],[813,4,14,42],[814,4,14,43],[815,4,14,44],[816,4,14,45],[817,4,14,46],[818,4,15,28],[819,4,15,29],[820,4,15,30],[821,4,15,31],[822,4,15,32],[823,4,15,33],[824,4,15,34],[825,4,15,35],[826,4,15,36],[827,4,15,37],[828,4,15,38],[829,4,15,39],[830,4,15,40],[831,4,15,41],[832,4,15,42],[833,4,15,43],[834,4,15,44],[835,4,15,45],[836,4,15,46],[837,4,16,28],[838,4,16,29],[839,4,16,30],[840,4,16,31],[841,4,16,32],[842,4,16,33],[843,4,16,34],[844,4,16,35],[845,4,16,36],[846,4,16,37],[847,4,16,38],[848,4,16,39],[849,4,16,40],[850,4,16,41],[851,4,16,42],[852,4,16,43],[853,4,16,44],[854,4,16,45],[855,4,16,46],[856,4,17,28],[857,4,17,29],[858,4,17,30],[859,4,17,31],[860,4,17,32],[861,4,17,33],[862,4,17,34],[863,4,17,35],[864,4,17,36],[865,4,17,37],[866,4,17,38],[867,4,17,39],[868,4,17,40],[869,4,17,41],[870,4,17,42],[871,4,17,43],[872,4,17,44],[873,4,17,45],[874,4,17,46],[875,4,18,28],[876,4,18,29],[877,4,18,30],[878,4,18,31],[879,4,18,32],[880,4,18,33],[881,4,18,34],[882,4,18,35],[883,4,18,36],[884,4,18,37],[885,4,18,38],[886,4,18,39],[887,4,18,40],[888,4,18,41],[889,4,18,42],[890,4,18,43],[891,4,18,44],[892,4,18,45],[893,4,18,46],[894,4,19,28],[895,4,19,29],[896,4,19,30],[897,4,19,31],[898,4,19,32],[899,4,19,33],[900,4,19,34],[901,4,19,35],[902,4,19,36],[903,4,19,37],[904,4,19,38],[905,4,19,39],[906,4,19,40],[907,4,19,41],[908,4,19,42],[909,4,19,43],[910,4,19,44],[911,4,19,45],[912,4,19,46],[913,4,20,28],[914,4,20,29],[915,4,20,30],[916,4,20,31],[917,4,20,32],[918,4,20,33],[919,4,20,34],[920,4,20,35],[921,4,20,36],[922,4,20,37],[923,4,20,38],[924,4,20,39],[925,4,20,40],[926,4,20,41],[927,4,20,42],[928,4,20,43],[929,4,20,44],[930,4,20,45],[931,4,20,46],[932,4,21,28],[933,4,21,29],[934,4,21,30],[935,4,21,31],[936,4,21,32],[937,4,21,33],[938,4,21,34],[939,4,21,35],[940,4,21,36],[941,4,21,37],[942,4,21,38],[943,4,21,39],[944,4,21,40],[945,4,21,41],[946,4,21,42],[947,4,21,43],[948,4,21,44],[949,4,21,45],[950,4,21,46],[951,4,22,28],[952,4,22,29],[953,4,22,30],[954,4,22,31],[955,4,22,32],[956,4,22,33],[957,4,22,34],[958,4,22,35],[959,4,22,36],[960,4,22,37],[961,4,22,38],[962,4,22,39],[963,4,22,40],[964,4,22,41],[965,4,22,42],[966,4,22,43],[967,4,22,44],[968,4,22,45],[969,4,22,46],[970,4,23,28],[971,4,23,29],[972,4,23,30],[973,4,23,31],[974,4,23,32],[975,4,23,33],[976,4,23,34],[977,4,23,35],[978,4,23,36],[979,4,23,37],[980,4,23,38],[981,4,23,39],[982,4,23,40],[983,4,23,41],[984,4,23,42],[985,4,23,43],[986,4,23,44],[987,4,23,45],[988,4,23,46],[989,4,24,28],[990,4,24,29],[991,4,24,30]];
// Available Filter_IDs from Product table
$items = [17,815,2411,839,400,1200,12,19,1,15,16,18,11,8,9,13,3,274,282,285,273,283,267,275,281,810,814,809,806,807,801,800,533,541,537,594,599,608,535,605,542,606,547,596,598,600,540,548,607,549,534,592,539,601,544,551,546,550,538,536,0,543,1067,1081,1074,1069,1070,1073,1079,1065,1066,1072,1336,1339,1335,1331,1333,1332,1338,1347,1340,1344,1342,1341,1345,1337,1871,1868,1863,1870,1872,1875,1877,1879,1869,1865,1878,2136,2139,2138,2131,2143,2134,2137,2135,2146,2133,2132,2130,2129,2140,2409,2404,2405,2403,2401,2396,2410,2412,2400,2413];
//Fillter_1 Numeric IDS are 1 To 13
//Fillter_2 Numeric IDS are 14 To 27
//Fillter_3 Numeric IDS are 28 To 46

This kind of approach to solve such tasks is too JS! Some fundamental back-end programming lessons would be helpful in the long run.
For this one I could recommend to re-consider your data model or at least the usage of it. Assuming you're using a proper relational ER-Model, it's better to avoid re-inventing the wheel and just use the DB query builder provided by the framework and leave the rest (counting etc...) to the existing PHP methods… Eventually no need for any primitive loops or similar complex solutions. So you'll have a convention-friendly code and won't need to attach the whole large file in the future as well!
Good Luck!

Related

laravel conditional join based on optional external keys

I got this db structure:
ddts contains 3 optional external keys (only one of panel_id, sawn_id or veneer_id can contains an external id and other 2 equals to null) .
So one ddt can be exatly just one of this 3 types:
sawn
panel
veneer
I need to extract for every company_id(another external key) i will sum some data from panels,sawns and veneers, but before sum it i need even to convert some of them in kgs too (with a function implemented by me).
In the ddts model ive the methods: panel(),sawn(),veneer();
I need just the final sum, but i guess that for achieve this i need to build up a huge collection and then manipulate it...
Id like to understand what is best to doing by query and what by code.
My first approach was about:
select all companies
in a foreach loop all ddts for each company
in a foreach loop for each ddt associate a type by selecting it
trough an if condition steatement
in a forache loop selection the value to sum
convert it to kgs where is necessary
sum it;
but it seems so long and im quite shure that the point 2 and 3 should be done by a JOIN but not so clare how!

Using Ajax to suggest unavaliable number using current list numbers

is it possible to using ajax with PHP/SQL to suggest the next avaliable number for a user based on the current numbers in the list? this is to avoid duplication.
I have a form/table http://i.stack.imgur.com/TYhSG.png and in this table I have for an example departments and a list(the numbers in the list needs to be unqiue). because of the way the system is disigned, users had to manually rearrange the numbers if they wanted to the same number in another department. for an example number "4" can only be used once, this example applies to any other numbers.
The numbers in those list are all avaliable in the database, what this means is that I have a select statement and i echo $list to redender those list you show in the image.
Tp avoid users having to manally tryping those numbers especially if the list is up to 100 or more. I basically want to use ajax to first check if the number in the list is already in used and if yes then suggest a number to them.
Can this be done? If so please show me an example.
Thanks for your time.
p.s.
i missed out an information piece of information they is another column called `values
tablename: check
values type list
cars 1 2
cars 1 4
cars 1 3
bicycle 1 2
bicycle 1 3
bicycle 1 4
the numbers in the list can be used again if the values is different so basically the list needs to be unique to the values is it in.
If the column list needs to be unique and automatically increment, you should be using MySQL's AUTO_INCREMENT
This will automatically populate the column with the next available number on row creation.

MySQL search in field (or other solutions)

I have a table with products that fall under specific categories, but the products within each category can contain multiple meta data tracking field
Table: products
id name category metadata
1 something 1 blue,red,purple
2 something else 2 left,right,middle
I have been trying to contemplate the best method to have a single product table but can't seem to squeeze the metadata in conveniently. for now I have created a table with all the metadata and fields for tracking the related category (the sequence is so i can order them withing a dropdown etc..)
Updated table: products
id name category metadata
1 something 1 1,2,3
2 something else 2 4,5,6
Table: metadata
id category sequence option
1 1 1 blue
2 1 2 red
3 1 3 purple
4 2 1 left
5 2 2 right
6 2 3 middle
If this format makes sense .. I am trying to generate a query that will search for values in my product table and grab each and all of the related meta values. The issue I am having is trying to find a unique value in the products field. if I do a MySQL search for LIKE(%1%) I will get matches for 1, 11, 21, 31 etc ... I thought of adding a leading and trailing comma to the field by default and then search for ",1," which would be unique .. but there has to be a better way ...
Any recommendations (regarding format or query)?
It's not an ideal design to have comma-separated values within a single database field. Aside from the problem you mentioned (difficult to search), your queries will be less efficient, as the DB won't be able to use indices for the lookup.
I'd recommend making a separate table products_metadata with a many-to-one relationship to the products table. Have the *metadata_id*, and the *product_id*, which is a foreign key linking back to the products table. That will make your job much easier.
You want to add another table, which links products to their metadata. It will have two columns: productid and metadataid which refer to the relevant entries in the products and metadata tables respectively. Then you no longer keep metadata in the products table, but JOIN them together as required.

Storing table references in text field (for Agile Toolkit)

I have been experimenting with RDB design lately, and I was wondering about storing items in a field that can have more than one value:
CARS Color_avail
1 corvette 1, 2, 3 <<<<<<<
2 ferrari 2
3 civic 1
COLORS
1 red
2 White
3 black
so on CRUD I would like to add more than one item via a drop down / checkboxes or something that would hold multiple values.
I can see the benefit of displaying the output like this in a form, but do you really want to store it like this in the database ?
For example with a datamodel that holds a comma separated list as in your example, what SQL would you use to identify all the cars available in white ?
The traditional way to hold a many to many relationship like this is to use an additional table e.g. you have a separate table that holds CAR_COLOUR with the following contents
CAR COLOUR
1 1
1 2
1 3
2 2
3 1
So now you can easily query things like, get a list of all cars and colours
SELECT CAR, COLOUR
FROM CARS CA,
COLOUR COL,
CAR_COLOUR CACOL
WHERE CA.CAR=CACOL.CAR
AND CACOL.COL=COL.COLOUR
OR if you just want the white cars, add the following to the WHERE clause
AND COL.COLOUR='White'
an index on the id fields and on both fields in CAR_COLOUR will mean you get great performance even if you have thousands of rows whereas putting them all in a comma separated list in a single field will mean you have to use substr or like which would prevent the use of indexes and mean as the amount of data grows, the performance will degrade rapidly.
Storing relations in the coma-separated list makes sense in some senses. You don't need commas though. There are 2 existing controls which can help you with that.
Displaying list of values with checkboxes in a form:
$form->addField('CheckboxList','corvette')->setValueList($array);
(you can populate array through $model->getRows() although I think it needs to be associative. You can probably join them with var_dump and foreach).
Your other options is to use a hidden field with selectable grid.
$field = $form->addField('line','selection');
$grid = $form->add('MVCGrid');
$grid->setModel('Colors',array('name'));
$grid->addSelectable($field);
$form->addSubmit();
To hide the actual field, you can either use "hidden" instead of "line" or use JavaScript to hide it:
$field->js(true)->hide();
or
$field->js(true)->closest('dl')->hide();
if you need to hide markup around the field too.

How to Handle Consuming Lots of Data from Multiple Sources in a Web SIte

This is a "meta" question that I am asking in a effort to better understand some tough nuts I've had to crack lately. Even if you don't get precisely what I'm reaching for here or there is too much text to read through, any practical input is appreciated and probably useful.
Assume you have a website that needs to use data that is stored in multiple tables of a database. That data will need to be iterated through in a multitude of ways, used for calculations in various places, etc.
So on a page that needs to display a collection of projects (from one db table) that each contain a collection of categories (from another db table) that each contain 1 or more items (from another db table) what is the best way to gather the data, organize it and iterate through it for display?
Since each project can have 1 or more categories and each category can have one or more items (but the items are unique to a specific category) what's the best way to organize the resulting pile?
My goal in the below example is to generate a table of projects where each project has the associated categories listed with it and each category has the associated items listed with it but I also need to aggregate data from the items table to display next to the project name
A Project Name (43 items and 2 of them have errors!)
- category 1
- item 1
- item 2
- category 2
- item 1
Another Project Name (12 items and no errors)
- category 1
- item 1
- category 2
- item 1
What I did was to retrieve the data from each table and stick it in a variable. Giving me something like:
var $projects = array("id" => 1, "proj_id" => 1, "name" => "aname");
var $categories = array("id" => 1, "cat_id" => 1234, "proj_id" => 1, "cat_name" => "acatname");
var $items = array("id" => 1, "item_id" => 1234, "location" => "katmandu");
Then I went through the variables in nested foreach() loops building the rows I needed to display.
I ran into difficulties with this as the foreach() loop would work fine when building something 2 levels deep (associating categories with projects) but it did not work as expected when went three levels deep (I N C E P T I O N .. hah, couldn't resist) and tried adding the items to each category (instead adding all of them to one item... first or last I don't recall which). Also, when something was present in the third level of the array, how would you add up that data and then get it out for use back up in the top level of the array being built?
I suppose I could have constructed a mega SQL query that did it all for me and put everything into a single array, saving me the loop confusion by flattening it out, but... well, that's why I'm here asking you all.
So, I suppose the heart of this question is: How do you handle getting lots of data from different tables and then combining it all for display and use in calculations?
Sounds like you're going to want to use SQL JOINs. Consider looking into them:
http://www.w3schools.com/sql/sql_join_left.asp
They'll pull data from multiple tables and aggregate it. It won't produce quite what you're looking for, but it will produce something that you can use in a different way.
is Hadoop the sort of thing you're looking for?

Categories