Goldeneye Posted October 30, 2009 Share Posted October 30, 2009 I have a categorization-class which works perfectly as it is. I've decided to modify this class to support sub-categories. Here's how I'm trying to do it: <?php $_foo = array('Parent 1' => array('Child 1-1', 'Child 1-2', 'Child 1-3', 'Child 1-4'), 'Parent 2' => array('Child 2-1', 'Child 2-2', 'Child 2-3') ); foreach($_foo as $key => $val){ $offset += 0; $categories = $category->generate($val, $offset); $num = array_keys($val); $offset = count($val); $mainb .= '<b>'.$key.'</b>'; $mainb .= '<ul style="margin-left: 2em;">'; for($i = 1; $i < $offset; ++$i) $mainb .= '<li><a href="categories.php?category='.$num[$i].'">'.$category->names($val, $num[$i]).'</a></li>'; $mainb .= '</ul>'; } ?> $offset is the number of elements of the current array to be used in the next pass through the loop for the $category->generate() function. Basically how it works is if $offset does not equal zero, it will use that value as the starting point for generating the numerated array-keys for each value in the array. Example 1: $offset = 0; $array = array('element 1', 'element 2', element 3'); The arrray from $category->generate($array, $offset) would then be: $array = array(1 => 'element 1', 2 => 'element 2', 4 => 'element 3'); Example 2: $offset = 3; $array = array('element 1', 'element 2', element 3'); The arrray from $category->generate($array, $offset) would then be: $array = array(8 => 'element 1', 16 => 'element 2', 32 => 'element 3'); Here are the functions I use in my Categorization-class: <?php class category { // $category::generate() takes an array of values and assigns each value a base-2 key. // IF $offset does NOT 0 (zero), the Array-Key will start off from that value (not 0) function generate($array, $offset = 0){ $n = count($array); $c = array(); for($i = 0; $i < $n; ++$i){ $x = $offset == 0 ? $i : $offset; $c[abs(pow(2, $x))] = $array[$i]; //if($offset != 0) ++$x; } return $c; } // category::names() takes the $array and retrieves the name corresponding the array-key given (as $num) function names($array, $num){ if($num==0) return 'None'; $category = $this->generate($array); foreach($category as $key => $val) if($num & $key) $names[] = $val; if(count($names) > 1) return implode(', ', $names); return $names[0]; } } ?> Basically what I want to do is loop through the two-dimensional array assign values to each of the values in the remaining arrays. This is what I want to achieve Parent 1: Array ( [1] => Child 1-1 [2] => Child 1-2 [4] => Child 1-3 [8] => Child 1-4 ) Parent 2: Array ( [16] => Child 2-1 [32] => Child 2-2 [64] => Child 2-3 ) But this is what I get instead Parent 1: Array ( [1] => Child 1-1 [2] => Child 1-2 [4] => Child 1-3 [8] => Child 1-4 ) Parent 2: Array ( [16] => Child 2-3 ) If anyone has any idea what exactly is going wrong here (or can suggest a better/simpler way) please share because this is getting to be a real obstacle for me right now. Hopefully this is clear enough. If it isn't I will do my best to try to clarify anything that might be unclear. A preemptive thanks! EDIT: Oh, and the browser-output I'm getting is: <h2>Parent 1</h2> <link>Child 1-1</link> <link>Child 1-2</link> <link>Child 1-1, Child 1-2</link> <h2>Parent 2</h2> <link>Child 2-1</link> <link>Child 2-2</link> Instead of: <h2>Parent 1</h2> <link>Child 1-1</link> <link>Child 1-2</link> <link>Child 1-3</link> <h2>Parent 2</h2> <link>Child 2-1</link> <link>Child 2-2</link> <link>Child 2-3</link> Link to comment https://forums.phpfreaks.com/topic/179677-solved-sub-category-system-using-2-d-arrays/ Share on other sites More sharing options...
marcus Posted October 30, 2009 Share Posted October 30, 2009 <?php $_foo = array('Parent 1' => array('Child 1-1', 'Child 1-2', 'Child 1-3', 'Child 1-4'), 'Parent 2' => array('Child 2-1', 'Child 2-2', 'Child 2-3')); foreach($_foo AS $key => $val){ echo "<h2>".$key."</h2>"; foreach($val AS $meow){ echo "<link>".$meow."</link><br>\n"; } } ?> I get: Parent 1 Child 1-1 Child 1-2 Child 1-3 Child 1-4 Parent 2 Child 2-1 Child 2-2 Child 2-3 Link to comment https://forums.phpfreaks.com/topic/179677-solved-sub-category-system-using-2-d-arrays/#findComment-948028 Share on other sites More sharing options...
Goldeneye Posted October 30, 2009 Author Share Posted October 30, 2009 <?php $_foo = array('Parent 1' => array('Child 1-1', 'Child 1-2', 'Child 1-3', 'Child 1-4'), 'Parent 2' => array('Child 2-1', 'Child 2-2', 'Child 2-3')); foreach($_foo AS $key => $val){ echo "<h2>".$key."</h2>"; foreach($val AS $meow){ echo "<link>".$meow."</link><br>\n"; } } ?> I get: Parent 1 Child 1-1 Child 1-2 Child 1-3 Child 1-4 Parent 2 Child 2-1 Child 2-2 Child 2-3 That actually helped narrow down the problem. It didn't solve my problem but I at least know the problem (most likely) resides with that offset variable I use in $category->generate(). So thank you very much! Link to comment https://forums.phpfreaks.com/topic/179677-solved-sub-category-system-using-2-d-arrays/#findComment-948066 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.