laravel conditional join based on optional external keys - php

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!

Related

Finding items in multiple arrays

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!

PHP, MySQL, Efficient tag-driven search algorithm

I'm currenlty building a webshop. This shop allows users to filter products by category, and a couple optional, additional filters such as brand, color, etc.
At the moment, various properties are stored in different places, but I'd like to switch to a tag-based system. Ideally, my database should store tags with the following data:
product_id
tag_url_alias (unique)
tag_type (unique) (category, product_brand, product_color, etc.)
tag_value (not unique)
First objective
I would like to search for product_id's that are associated with anywhere between 1-5 particular tags. The tags are extracted from a SEO-friendly url. So I will be retrieving a unique strings (the tag_url_alias) for each tag, but I won't know the tag_type.
The search will be an intersection, so my search should return the product_id's that match all of the provided tags.
Second objective
Besides displaying the products that match the current filter, I would also like to display the product-count for other categories and filters which the user might supply.
For instance, my current search is for products that match the tags:
Shoe + Black + Adidas
Now, a visitor of the shop might be looking at the resulting products and wonder which black shoes other brands have to offer. So they might go to the "brand" filter, and choose any of the other listed brands. Lets say they have 2 different options (in practice, this will probably have many more), resulting in the following searches:
Shoe + Black + Nike > 103 results
Shoe + Black + K-swiss > 0 results
In this case, if they see the brand "K-swiss" listed as an available choise in their filter, their search will return 0 results.
This is obviously rather disappointing to the user... I'd much rather know that switching the "brand" from "adidas" to "k-swiss" will 0 results, and simply remove the entire option from the filter.
Same thing goes for categories, colors, etc.
In practice this would mean a single page view would not only return the filtered product list described in my primary objective, but potentially hundreds of similar yet different lists. One for each filter value that could replace another filter value, or be added to the existing filter values.
Capacity
I suspect my database will eventually contain:
between 250 and 1.000 unique tags
And it will contain:
between 10.000 and 100.000 unique products
Current Ideas
I did some Google searches and found the following article: http://www.pui.ch/phred/archives/2005/06/tagsystems-performance-tests.html
Judging by that article, running hundreds of queries to achieve the 2nd objective, is going to be a painfully slow route. The "toxy" example might work for my needs and it might be acceptable for my First objective, but it would be unacceptably slow for the Second objective.
I was thinking I might run individual queries that match 1 tag to it's associated product_id's, cache those queries, and then calculate intersections on the results. But, do I calculate these intersections in MySQL? or in PHP? If I use MySQL, is there a particular way I should cache these individual queries, or is supplying the right indexes all I need?
I would imagine it's also quite possible to maybe even cache the intersections between two of these tag/product_id sets. The amount of intersections would be limited by the fact that a tag_type can have only one particular value, but I'm not sure how to efficiently manage this type of caching. Again, I don't know if I should do this in MySQL or in PHP. And if I do this in MySQL, what would be the best way to store and combine this type of cached results?
Using sphinx search engine can make this magic for you. Its is VERY fast, and even can handle wordforms, what can be useful with SEO requests.
In terms of sphinx, make a document - "product", index by tags, choose proper ranker for query (ex, MATCH_ALL_WORDS) and run batch request with different tag combinations to get best results.
Dont forget to use cachers like memcahed or any other.
I did not test this yet, but it should be possible to have one query to satisfy your second objective rather than triggering several hundred queries...
The query below illustrates how this should work in general.
The idea is to combine the three different requests at once and group by the dedicated value and collect only those which have any results.
SELECT t1.product_id, count(*) FROM tagtable t1, tagtable t2, tagtable t3 WHERE
t1.product_id = t2.product_id AND
t2.product_id = t3.product_id AND
t1.tag_type='yourcategoryforShoe' AND t1.tag_value='Shoe' AND
t2.tag_type='product_color' AND t2.tag_value='Black' AND
t3.tag_type='brand'
GROUP BY t3.tag_value
HAVING count(*) > 0

PHP/MYSQL store variables in array or separate fields

Premature optimization is the root of all evil...but...
I am allowing users to input data within categories as in favorite players, favorite teams etc. They can then use these choices to filter results. I let them input lists separated by commas so after exploding the data I have it in an array. So how to store.
Method 1: I could create a table of users, one row per user, with the categories, as in players, teams as fields and save the choices of each users as an array in the respective field. (userid would link to basic users table.)
Method 2. Or I could create separate tables for each thing, players, teams, etc, and have a fixed number of fields say 10, break up the array into each individual value, store and place it in its own field. (Already have this code working.) (Again userid is primary key.)
The advantage of Method 1 is it's a bit simpler, one table, no limit on number of choices.
Method 2 seems a bit more robust. The data is more visible and possibly easier to get and retrieve--although maybe not.
Does anyone have experience with this sort of thing and could recommend one over another?
Thanks for any recommendations, suggestions!

Creating columns of equal width

So I got a problem that I can't wrap my mind around.
I'm creating a shopping list that is divided into ten categories of various lengths. (All of the items come from a database). I got it to work when using a single column, but I have to divide the list into four columns. The code should decide which categories should go where so that the four columns have the most equal number of items possible.
This is what the list will look like when the code is working.
Out of these ten categories, four of them have a specific category they belong to.
The way I've approached this is to count the total number of items and divide it by four to compute the average number of items per column. I put the four special categories in their respective column and kept track of how many items were now in each column.
Now I still have six columns remaining of various sizes. What is the best approach to put them in the column that would fit best? Since some categories are much larger than others, some columns could potentially have three or four categories.
UPDATE: Right after I posted this I came to the realization that I should find the column with the least items and add the largest category to it. This seems like it will work. And it looks like Dave is suggesting the same!
After writing your 4 "main" categories to the columns, make an array that has a total of each column:
$columnTotals = array(10,6,12,13)
//example - obviously you'd use count or something to get the totals
Then, order your non-special categories in an array by largest to smallest:
$subcatTotals = array(18,15,13,12,8,4);
//here, you'll have to get the totals, then use an array sort to order them
//probably want an associative array so you know which total matches which cat.
Then, in a loop, add the first(largest) sub-category to the smallest column, and get a new total for that column.
This SHOULD give you the most even columns you can get - at least it has in all the made-up examples I've tried it with.
Your approach is most ideal in today's context. Let me explain...
The ideal thing to do right now is do your little calculation and split the list into the number of rows & columns.
The alternative is a CSS3 approach. i.e., you can create the whole list in ONE column through PHP. And on the CSS side, you can specify the new property "column-count".
But there are issues. This is not yet properly standardised. So you've got to specify the -moze- prefix and -webkit- prefix depending on your browser. But the reason I wouldn't go for this is that IE still does not support this. And it's too early to consider an upgrade by all users even if they did.
Going one step further, you ought to modify your splitting algorithm to take into account the category headings.
Hope this helps :)

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