Jump to content

looking to fit a while loop instead of a foreach loop here


co.ador

Recommended Posts

<?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();
    }?>

 

 

 

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.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.