djones Posted June 28, 2010 Share Posted June 28, 2010 I'm struggling with how to dynamically loop through a product's list of options. Let's day I have the variations, Size(variation_id = 2) and Color(variation_id = 4) assigned to product A. Which produces these arrays: // Sizes array(4) { [0]=> string(2) "SM" [1]=> string(3) "MED" [2]=> string(2) "LG" [3]=> string(3) "XXL" } // Colors array(3) { [0]=> string(4) "Blue" [1]=> string(5) "Green" [2]=> string(3) "Red" } I know I can hard code the loop, but that will not work if I add another variation. How can I build the loop to work with any amount of variations? // Get the variations from the product_id A // $sql = "SELECT variation_id FROM product_variations WHERE product_id = 'A' / ... // I can query for the variations and loop through to get the variation values // ... // $set1 is the Sizes array from mysql query // $set2 is the Colors array from mysql query $count1 = count($set1); $count2 = count($set2); for ($a=0; $a < $count1; $a++){ for ($b=0; $b < $count2; $b++){ $options = $set1[$a].' : '.$set2[$b]; echo $options.'<br />'; } } // Output /* SM : Blue SM : Green SM : Red MED : Blue MED : Green MED : Red LG : Blue LG : Green LG : Red XXL : Blue XXL : Green XXL : Red */ Link to comment https://forums.phpfreaks.com/topic/206096-help-with-product-optionsvariations-array/ Share on other sites More sharing options...
djones Posted June 30, 2010 Author Share Posted June 30, 2010 Any ideas or suggestions? Link to comment https://forums.phpfreaks.com/topic/206096-help-with-product-optionsvariations-array/#findComment-1078965 Share on other sites More sharing options...
djones Posted July 1, 2010 Author Share Posted July 1, 2010 A friend was able to help. I'm posting it here for others to see. // Put each variation set into an array // i.e $set[0] is your Sizes, $set[1] is your Colors /* Sizes array(4) { [0]=> string(2) "SM" [1]=> string(3) "MED" [2]=> string(2) "LG" [3]=> string(3) "XXL" } Colors array(3) { [0]=> string(4) "Blue" [1]=> string(5) "Green" [2]=> string(3) "Red" } */ $options = array(); $current_index_path = array(); $variations = -1; function variation_matrix($variations, $current_index_path, &$options, &$set) { $variations++; if ( $variations == count($set) - 1 ) { //you are at the last group for ( $i = 0; $i < count($set[$variations]); $i++ ) { $option_string = ""; for ( $j = 0; $j < count($current_index_path); $j++) { $option_string .= $set[$j][$current_index_path[$j]]. " : "; } $option_string .= $set[$variations][$i]; $options[] = $option_string; } } else { for ( $i = 0; $i < count($set[$variations]); $i++ ) { $cip_copy = $current_index_path; $cip_copy[] = $i; //append index from this variation variation_matrix($variations, $cip_copy, &$options, &$set); //dig deeper } } } variation_matrix($variations, $current_index_path, &$options, &$set); /* Output array(12) { [0]=> string(9) "SM : Blue" [1]=> string(10) "SM : Green" [2]=> string( "SM : Red" [3]=> string(10) "MED : Blue" [4]=> string(11) "MED : Green" [5]=> string(9) "MED : Red" [6]=> string(9) "LG : Blue" [7]=> string(10) "LG : Green" [8]=> string( "LG : Red" [9]=> string(10) "XXL : Blue" [10]=> string(11) "XXL : Green" [11]=> string(9) "XXL : Red" } */ Link to comment https://forums.phpfreaks.com/topic/206096-help-with-product-optionsvariations-array/#findComment-1079435 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.