co.ador Posted April 22, 2011 Share Posted April 22, 2011 <?php $hierachy = new hierachy; $iterator = new RecursiveIteratorIterator(new recursiveArrayIterator($hierachy->getLocalSubNodes($name1))); try { foreach($iterator as $key=>$value) { echo $value = substr($value,0,-1).'<br />'; } } catch(Exception $e) { echo $e->getMessage(); }?> Quote Link to comment https://forums.phpfreaks.com/topic/234463-looking-to-fit-a-while-loop-instead-of-a-foreach-loop-here/ Share on other sites More sharing options...
fugix Posted April 22, 2011 Share Posted April 22, 2011 can i ask why you want to change the type of loop? Quote Link to comment https://forums.phpfreaks.com/topic/234463-looking-to-fit-a-while-loop-instead-of-a-foreach-loop-here/#findComment-1204984 Share on other sites More sharing options...
co.ador Posted April 22, 2011 Author Share Posted April 22, 2011 Functions file <?php public function getLocalSubNodes($node_name){ $stmt = conn::getInstance()->prepare(" SELECT node.name, (COUNT(parent.name) - (sub_tree.depth + 1)) AS depth FROM categories AS node, categories AS parent, categories AS sub_parent, ( SELECT node.name, (COUNT(parent.name) - 1) AS depth FROM categories AS node, categories AS parent WHERE node.left_node BETWEEN parent.left_node AND parent.right_node AND node.name = :node_name GROUP BY node.name ORDER BY node.left_node )AS sub_tree WHERE node.left_node BETWEEN parent.left_node AND parent.right_node AND node.left_node BETWEEN sub_parent.left_node AND sub_parent.right_node AND sub_parent.name = sub_tree.name GROUP BY node.name HAVING depth <= 1 ORDER BY node.left_node"); $stmt->bindParam(':node_name', $node_name, PDO::PARAM_STR); $stmt->execute(); return $stmt->fetchALL(PDO::FETCH_ASSOC); } ?> I instantiate the class calling the getLocalSubNodes functions <?php $hierachy = new hierachy; $iterator = new RecursiveIteratorIterator(new recursiveArrayIterator($hierachy->getLocalSubNodes($name1))); try { foreach($iterator as $key=>$value) { echo $key.' -- '.$value.'<br />'; } } catch(Exception $e) { echo $e->getMessage(); } ?> resulting in name -- mp3 players depth -- 1 name -- cd players depth -- 1 name -- 2 way radios depth -- 1 I only want the name -- mp3 players name -- cd players name -- 2 way radios want to get rid of the depth. in the database I don't have such a field called depth it seems to be generated in the query above, I need the query to calculate the depth but I don't want to print the depth and its value, Don't need the depth and its value to be printed or echo. so I considered it could be the for each loop. not sure if that's the issue here though. thanks. Quote Link to comment https://forums.phpfreaks.com/topic/234463-looking-to-fit-a-while-loop-instead-of-a-foreach-loop-here/#findComment-1204996 Share on other sites More sharing options...
co.ador Posted April 22, 2011 Author Share Posted April 22, 2011 little if condition on within the foreach loop got rid of the depth field if($key <> "depth") { } Quote Link to comment https://forums.phpfreaks.com/topic/234463-looking-to-fit-a-while-loop-instead-of-a-foreach-loop-here/#findComment-1205092 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.